...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
The BOOST_AUTO
macro emulates
the proposed auto
keyword in
C++.
BOOST_AUTO(var,expr) BOOST_AUTO_TPL(var,expr)
Arguments
a variable to be initialized with the expression
a valid c++ expression
If you want to use auto
in a
template-context, use BOOST_AUTO_TPL(expr)
,
which takes care of the typename
keyword inside the auto
expression.
int main() { length::meter a(5); force::newton b(6); BOOST_AUTO(c, a * b); }
The BOOST_TYPEOF_COMPLIANT
macro can be used to force the emulation mode. Define it if your compiler
by default uses another mode, such as native typeof
or Microsoft-specific trick, but you want to use the emulation mode, for
example for portability reasons.
The BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP
macro ensures that type registrations in different header files receive unique
identifiers.
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
specified once in every cpp/hpp file where any registration is performed, before any registration.
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP() class X; BOOST_TYPEOF_REGISTER_TYPE(X)
The BOOST_TYPEOF_INTEGRAL
macro is used when registering an integral template parameter using BOOST_TYPEOF_REGISTER_TEMPLATE
.
Useful for enum
s and dependent
integral template parameters.
BOOST_TYPEOF_INTEGRAL(x)
Arguments
a fully qualified integral type or enum
A short syntax has been implemented for the built in types (int, bool, long, unsigned long, etc.) Other non-type template parameters (e.g. pointer to member) are not supported.
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP() namespace foo { enum color {red, green, blue}; template<color C0,typename T1> class class_with_enum {}; template<typename T0,T0 I1> class class_with_dependent_non_type {}; } BOOST_TYPEOF_REGISTER_TEMPLATE(foo::class_with_enum, (BOOST_TYPEOF_INTEGRAL(foo::color)) (typename) ) BOOST_TYPEOF_REGISTER_TEMPLATE(foo::class_with_dependent_non_type, (typename) (BOOST_TYPEOF_INTEGRAL(P0)) )
The BOOST_TYPEOF_LIMIT_FUNCTION_ARITY
macro defines how many parameters are supported for functios, and applies
to functions, function pointers, function references, and member function
pointers. The default value is 10. Redefine if you want the Typeof Library
to handle functions with more parameters.
Define BOOST_TYPEOF_MESSAGE
before including boost/typeof/typeof.hpp to include messages "using
typeof emulation" and "using native typeof". By default, these
messages will not be displayed.
The BOOST_TYPEOF_LIMIT_SIZE
macro defines the size of the compile-time sequence used to encode a type.
The default value is 50. Increase it if you want the Typeof Library to handle
very complex types, although this possibility is limited by the maximum number
of template parameters supported by your compiler. On the other hand, if
you work only with very simple types, decreasing this number may help to
boost compile-time performance.
The BOOST_TYPEOF_REGISTER_TYPE
macro informs the Typeof Library about the existence of a type
BOOST_TYPEOF_REGISTER_TYPE(x)
Arguments
a fully qualified type
Must be used in the global namespace
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP() namespace foo { class bar {}; enum color {red, green, blue}; } BOOST_TYPEOF_REGISTER_TYPE(foo::bar) BOOST_TYPEOF_REGISTER_TYPE(foo::color)
The BOOST_TYPEOF_REGISTER_TEMPLATE
macro informs the Typeof Library about the existence of a template and describes
its parameters
BOOST_TYPEOF_REGISTER_TEMPLATE(x, n) BOOST_TYPEOF_REGISTER_TEMPLATE(x, seq)
Arguments
a fully qualified template
the number of template arguments. Only valid if all template arguments are typenames
a sequence of template arguments. Must be used when integral or template template parameters are present
Must be used in the global namespace.
The library allows registration of templates with type, integral, and template template parameters:
(class)
or (typename)
sequence element
(unsigned int)
. The following well-known integral types
are supported:
[signed/unsigned] char
[unsigned] short
[unsigned] int
[unsigned] long
unsigned
bool
size_t
BOOST_TYPEOF_INTEGRAL
macro, like (BOOST_TYPEOF_INTEGRAL(MyEnum))
BOOST_TYPEOF_TEMPLATE
macro, like: (BOOST_TYPEOF_TEMPLATE((class)(unsigned int)))
.
In case of all type parameters this can be shortened to something like
(BOOST_TYPEOF_TEMPLATE(2))
.
The nested template template parameters are not supported.
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP() namespace foo { template<typename T0, typename T1> class simple_template {}; template<typename T0, int I1> class class_with_integral_constant {}; } BOOST_TYPEOF_REGISTER_TEMPLATE(foo::simple_template, 2) BOOST_TYPEOF_REGISTER_TEMPLATE(foo::class_with_integral_constant, (typename)(int))
The BOOST_TYPEOF_TEMPLATE
macro is used when registering template template parameters using BOOST_TYPEOF_REGISTER_TEMPLATE
.
BOOST_TYPEOF_TEMPLATE(n) BOOST_TYPEOF_TEMPLATE(seq)
Arguments
the number of template arguments. Only valid if all template arguments are typenames
a sequence of template arguments. Must be used when there are integral constants in the nested template
Can not be used to register nested template template parameters.
#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP() namespace foo { enum color {red, green, blue}; template<color C0, template<typename> class T1> class nested_template_class {}; template<template<typename, unsigned char> class T1> class nested_with_integral {}; } BOOST_TYPEOF_REGISTER_TEMPLATE(foo::nested_template_class, (foo::color) (BOOST_TYPEOF_TEMPLATE(1)) ) BOOST_TYPEOF_REGISTER_TEMPLATE(foo::nested_with_integral, (BOOST_TYPEOF_TEMPLATE((typename)(unsigned char))) )
The BOOST_TYPEOF
macro calculates
the type of an expression, but removes the top-level qualifiers, const&
BOOST_TYPEOF(expr) BOOST_TYPEOF_TPL(expr)
Arguments
a valid c++ expression that can be bound to const T&
If you want to use typeof
in a template-context, use BOOST_TYPEOF_TPL(expr)
,
which takes care of typename
inside the typeof
expression.
template<typename A, typename B> struct result_of_conditional { typedef BOOST_TYPEOF_TPL(true?A():B()) type; }; template<typename A, typename B> result_of_conditional<A, B>::type min(const A& a,const B& b) { return a < b ? a : b; }
The TYPEOF_NESTED_TYPEDEF
macro works in much the same way as the 'TYPEOF' macro does, but workarounds
several compiler deficiencies.
BOOST_TYPEOF_NESTED_TYPEDEF(name,expr) BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr)
Arguments
a valid identifier to nest the typeof operation inside
expr
a valid c++ expression that can be bound to const T&
'typeof_nested_typedef' nests the 'typeof' operation inside a struct. By
doing this, the 'typeof' operation can be split into two steps, deconfusing
several compilers (notably VC7.1 and VC8.0) on the way. This also removes
the limitation imposed by BOOST_TYPEOF_LIMIT_SIZE
and allows you to use 'typeof' on much larger expressions.
If you want to use typeof_nested_typedef
in a template-context, use BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr)
,
which takes care of typename
inside the typeof
expression.
'typeof_nested_typedef' can not be used at function/block scope.
template<typename A, typename B> struct result_of_conditional { BOOST_TYPEOF_NESTED_TYPEDEF_TPL(nested,true?A():B()) typedef typename nested::type type; }; template<typename A, typename B> result_of_conditional<A, B>::type min(const A& a,const B& b) { return a < b ? a : b; }