Specializing C++ template based on presence/absense of a class member?

后端 未结 2 560
刺人心
刺人心 2020-12-16 23:22

Consider the following:

struct A {
  typedef int foo;
};

struct B {};

template
struct C {};

I wa

2条回答
  •  一个人的身影
    2020-12-16 23:28

    Something like this might help: has_member.

    typedef char (&no_tag)[1]; 
    typedef char (&yes_tag)[2];
    
    template< typename T > no_tag has_member_foo_helper(...);
    
    template< typename T > yes_tag has_member_foo_helper(int, void (T::*)() = &T::foo);
    
    template< typename T > struct has_member_foo {
        BOOST_STATIC_CONSTANT(bool
            , value = sizeof(has_member_foo_helper(0)) == sizeof(yes_tag)
            ); }; 
    
    template::value> 
    struct C {};
    

提交回复
热议问题