Provide a nil type.
More...
Detailed Description
- Outline
-
-
- Purpose:
- Provide a nil type.
-
- Classes:
-
-
- Description:
bslmf::Nil
is mainly used for template meta-programming. It is useful for providing defaults for template parameters and terminating template recursions. bslmf::Nil
can also be used to represent an unset state (i.e., nil) in components that require one.
-
- Usage:
- The following usage illustrates a typical use of
bslmf::Nil
as a default template parameter for another meta-function. template <int CONDITION,
class TRUE_TYPE = bslmf::Nil,
class FALSE_TYPE = bslmf::Nil>
struct my_If {
typedef TRUE_TYPE Type;
};
template <class TRUE_TYPE, class FALSE_TYPE>
struct my_If<0, TRUE_TYPE, FALSE_TYPE> {
typedef FALSE_TYPE Type;
};
Next, we can use the my_If
meta-function to implement a print function that checks whether a type has a print method (using a fictitious meta-function my_IsPrintable
), and print the value. Note the use of bslmf::Nil
as the type for overloading: template <class TYPE>
void print(const TYPE& value, int)
{
bsl::cout << value << bsl::endl;
}
template <class TYPE>
void print(const TYPE&, bslmf::Nil)
{
bsl::cout << "Cannot be printed." << bsl::endl;
}
template <class TYPE>
void print(const TYPE& value)
{
my_If<my_IsPrintable<TYPE>::value, int>::Type Type;
print(value, Type());
}
Using the above functions, the following code: Will print the following to stdout
: Finally, the bslmf::IsNil
meta-function returns true if the type passed to it is bslmf::Nil
, and false otherwise:
Define Documentation
Typedef Documentation