1

I'm trying to detect at compile time whether a class Foo or Bar has either the variable value or member variable initValue().

struct Foo
{
    static const int value;
    static int initValue();
};

struct Bar
{
};

I've found several boost and boost MPL classes and utils such as BOOST_MPL_HAS_XXX_TRAIT_DEF and valid_member_metafunction that appear to do just this but am unsure which to use. I know it's possible to roll out my own detector but I'd rather not.

sehe
  • 374,641
  • 47
  • 450
  • 633
Olumide
  • 5,397
  • 10
  • 55
  • 104
  • 1
    [Check is a member exists](https://stackoverflow.com/questions/1005476/how-to-detect-whether-there-is-a-specific-member-variable-in-class) and [check if a method exists](https://stackoverflow.com/questions/11249199/is-there-a-way-to-check-if-a-member-exists-in-a-struct); put them together with `std::enable_if` – David Apr 28 '14 at 14:02
  • @Dave Thanks but I'd rather not roll out my own. – Olumide Apr 28 '14 at 14:06

1 Answers1

2

Found it. has_static_member_data and has_static_member_function from the Boost TTI library.

#include <boost/tti/has_static_member_data.hpp>
BOOST_TTI_HAS_STATIC_MEMBER_DATA( value ) // Generates class template has_static_member_data_value
...
has_static_member_data_value<Foo, int>::value;  // returns true
has_static_member_data_value<Bar, int>::value;  // returns false
Olumide
  • 5,397
  • 10
  • 55
  • 104