...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_DECLARE_HAS_MEMBER macro allows to declare a trait which would then let introspect the existence of a class member (a data member or a member function) with the specified name.
For example, the following declarations introduce local::has_begin
and local::has_funop
traits which then allow to test
if the supplied class has respectively begin
and operator()
members:
namespace { namespace local { BOOST_DECLARE_HAS_MEMBER(has_begin, begin); BOOST_DECLARE_HAS_MEMBER(has_funop, operator()); }}
Now these traits can be used as follows:
namespace { namespace local { struct test01 { int begin; }; struct test02 { char* begin() { return 0; } }; struct test22 { void operator()() {} }; }}
BOOST_TEST(local::has_begin<local::test01>::value == true); BOOST_TEST(local::has_begin<local::test02>::value == true); BOOST_TEST(local::has_funop<local::test22>::value == true);
As it can be seen from the example the traits only check for the existence of a member (be that a data member or a member function) with the specified name.