BDE 4.14.0 Production release
Loading...
Searching...
No Matches

Macros

#define bslmf_IsNil   bslmf::IsNil
 This alias is defined for backward compatibility.
 

Typedefs

typedef bslmf::Nil bslmf_Nil
 This alias is defined for backward compatibility.
 

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;
};
This struct is empty and represents a nil type.
Definition bslmf_nil.h:131

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; // default false
// type is
// 'bslmf::Nil'.
print(value, Type());
}
bsl::ostream & print(bsl::ostream &stream, const TYPE &object, int level=0, int spacesPerLevel=4)
Definition bdlb_printmethods.h:719

Using the above functions, the following code:

int x = 3;

Will print the following to stdout:

3
Cannot be printed.

Finally, the bslmf::IsNil meta-function returns true if the type passed to it is bslmf::Nil, and false otherwise:

Definition bslmf_nil.h:139

Macro Definition Documentation

◆ bslmf_IsNil

#define bslmf_IsNil   bslmf::IsNil

Typedef Documentation

◆ bslmf_Nil