BDE 4.14.0 Production release
|
Provide a meta-function to map integral constants to unique types.
This component defines a simple template structure used to map an integral constant to a C++ type. bslmf::MetaInt<int>
defines a different type for each distinct compile-time constant integral parameter. That is, instantiations with different integer values form distinct types, so that bslmf::MetaInt<0>
is a distinct type from bslmf::MetaInt<1>
, which is also distinct from bslmf::MetaInt<2>
, and so on.
This section illustrates intended usage of this component
The most common use of this structure is to perform static function dispatching based on a compile-time calculation. Often the calculation is nothing more than a simple predicate, allowing us to select one of two functions. The following function, doSomething
, uses a fast implementation (e.g., memcpy
) if the parameterized type allows for such operations, otherwise it will use a more generic and slower implementation (e.g., copy constructor).
The power of this approach is that the compiler will compile only the implementation selected by the MetaInt
argument. For some parameter types, the fast version of doSomethingImp
would be ill-formed. This kind of compile-time dispatch prevents the ill-formed version from ever being instantiated.
In addition to forming new types, the value of the integral parameter to MetaInt
is "saved" in the enum member VALUE
, and is accessible for use in compile-time or run-time operations.