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

Macros

#define bslmf_TypeRep   bslmf::TypeRep
 This alias is defined for backward compatibility.
 

Typedefs

typedef bslmf::MatchAnyType bslmf_AnyType
 This alias is defined for backward compatibility.
 

Detailed Description

Outline

Purpose

Provide a generic type to which any type can be converted.

Classes

Description

bslmf::MatchAnyType is a type to which any type can be implicitly converted. This is useful for creating an overloaded function that is a catch-all for all types not explicitly provided for in other overloaded functions with the same name.

bslmf::TypeRep allows one to create a reference to a type. In complex template programming, one is often dealing with unknown types, about the constructors of which one knows nothing. One often needs an object of the given type, but since nothing is known about the constructors, one can't just construct and object of the type. bslmf::TypeRep allows one to create a reference to the type. Note that the rep function in bslmf::TypeRep is not implemented, it must never be called at run time.

Usage

This section illustrates intended use of this component.

Example 1: bslmf::MatchAnyType

struct X { };
struct Y { };
struct Z : public Y { };
inline bool isY(const bslmf::MatchAnyType&) { return false; }
inline bool isY(const Y&) { return true; }
assert(! isY(X()));
assert( isY(Y()));
assert( isY(Z()));
assert(! isY(int()));
assert(! isY(4));
assert(! isY(4.0));
assert(! isY("The king is a fink!"));
X x;
Y y;
Z z;
assert(! isY(x));
assert( isY(y));
assert( isY(z));
Any type can be converted into this type.
Definition bslmf_matchanytype.h:150

Example 2: bslmf::TypeRep

struct X {};
struct Y {};
struct HasHorridCtorX : public X {
HasHorridCtorX(int, double, X, char, char *, void *) {}
// It's inconvenient to actually create an object of this type
// because the constructor takes so many arguments. It's also
// impossible because the c'tor is undefined.
};
struct HasHorridCtorY : public Y {
HasHorridCtorY(int, double, X, char, char *, void *) {}
// It's inconvenient to actually create an object of this type
// because the constructor takes so many arguments. It's also
// impossible because the c'tor is undefined.
};
template <int i>
struct MyMetaInt { char d_array[i + 1]; };
#define METAINT_TO_UINT(mymetaint) (sizeof(mymetaint) - 1)
MyMetaInt<1> isX(const X&);
MyMetaInt<0> isX(const bslmf::MatchAnyType&);
assert(1 == METAINT_TO_UINT(isX(X())));
assert(0 == METAINT_TO_UINT(isX(Y())));
assert(1 == METAINT_TO_UINT(isX(bslmf::TypeRep<HasHorridCtorX>::rep())));
assert(0 == METAINT_TO_UINT(isX(bslmf::TypeRep<HasHorridCtorY>::rep())));
assert(0 == METAINT_TO_UINT(isX(3)));
assert(0 == METAINT_TO_UINT(isX(3.0)));
assert(0 == METAINT_TO_UINT(isX("The king is a fink!")));
Generate a reference to t_TYPE for use in meta-functions.
Definition bslmf_matchanytype.h:189

Macro Definition Documentation

◆ bslmf_TypeRep

#define bslmf_TypeRep   bslmf::TypeRep

Typedef Documentation

◆ bslmf_AnyType