|
BDE 4.14.0 Production release
|
Macros | |
| #define | bslmf_If bslmf::If |
| This alias is defined for backward compatibility. | |
Provide a compile-time if/else (conditional) meta-function.
This component contains the template class meta-function bslmf::If that is parameterized on three arguments. The first argument is a (compile-time constant) integral expression that is interpreted by the meta-function as a boolean conditional; the other two parameters accept type arguments. If the value of the first argument is non-zero (true), the meta-function "returns" its second argument (i.e., corresponding to the first type parameter); otherwise it returns its third argument (the second type parameter). If the selected type argument is not explicitly specified, the meta-function returns the default bslmf::Nil type.
A meta-function is a class template that evaluates, at compile-time, to one or more types and values. An example of a simple meta-function that adds two (compile-type constant) integer values is the following Plus class template:
The initializer of the VALUE enumerator is the compile-time summation of the constant values A and B. The result "returned" by Plus is provided by the VALUE enumerator.
An example where a type is returned from a meta-function rather than a value is illustrated by SelectLarger below. The SelectLarger meta-function selects the larger of two types. The result "returned" by SelectLarger is provided by the SelectLarger<...>::Type typedef:
The preceding two examples illustrate the naming conventions used throughout bslmf to denote the types and values returned by meta-functions. In particular, enumerators or static const integral variables named VALUE provide the results of value-returning meta-functions, and nested types named Type provide the results of type-returning meta-functions.
The following snippets of code illustrate basic use of the bslmf::If meta-function. The examples make use of the following declarations to identify the type that is selected by a given constant integral expression:
In the following example, the meta-function condition (the first argument to bslmf::If) evaluates to true (non-zero). Thus, bslmf::If<...>::Type is a synonym for int; i.e., it "evaluates" (at compile time) to int:
In the next example, the condition argument evaluates to false (zero). In this case, bslmf::If<...>::Type evaluates to bslmf::Nil since the third template argument (the "else" type) is not explicitly specified:
| #define bslmf_If bslmf::If |