Quick Links:

bal | bbl | bdl | bsl

Namespaces | Defines | Typedefs

Component bslmf_matchanytype
[Package bslmf]

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

Namespaces

namespace  bslmf

Defines

#define bslmf_TypeRep   bslmf::TypeRep

Typedefs

typedef bslmf::MatchAnyType bslmf_AnyType

Detailed Description

Outline
Purpose:
Provide a generic type to which any type can be converted.
Classes:
bslmf::MatchAnyType generic type to which any type can be converted
bslmf::TypeRep meta-function for providing a reference to t_TYPE
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));
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 MetaInt { char d_array[i + 1]; };

  #define METAINT_TO_UINT(metaint)   (sizeof(metaint) - 1)

  MetaInt<1> isX(const X&);
  MetaInt<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!")));

Define Documentation

#define bslmf_TypeRep   bslmf::TypeRep

Typedef Documentation