使用 SFINAE select friend 或 base class c++x03

Using SFINAE to select friend or base class c++x03

您好,我正在尝试使用 SFINAE 检查 class 上是否存在变量并使用它来定义我应该使用哪个朋友,我发现当我检查 [=21] 的值时=] 它无法扣除实际值。这是预期的吗? 我怎么能实现类似的东西? 这样的输出结果应该是:

0
1

但是当我再次放置朋友时它会是

1
1

我在 gcc 上使用 c++03。

#include <iostream>
#include <string>

template <typename T>
class has_helloworld
{
    template <typename C> static char (&f( __typeof__(&C::helloworld) ) )[1] ;
    template <typename C> static char (&f(...))[2];
public:
    static const bool value = sizeof(f<T>(0)) == 2 ;
};
template <class T, const bool>
class FriendOption{public: void t (){std::cout<< "genericFriend";  }};
template <class T>
class FriendOption<T, false> {public: void t (){std::cout<< "false friend";  }};
template <typename T>
class Hello 
{
        typedef T field_type;
static const bool test = has_helloworld<field_type>::value;
    //friend class FriendOption<field_type, test >;
public:
    int helloworld() { return 0; }
};
class OTF {
public:
    int helloworld() { return 0; }
};
class test : public Hello <test>
{};

struct Generic {};

int main(int argc, char *argv[])
{
    std::cout << has_helloworld<test>::value << std::endl;
    std::cout << has_helloworld<Generic>::value << std::endl;
    return 0;
}

问题不在朋友而是在测试

static const bool test = has_helloworld<field_type>::value

通过全新类型Hello<field_type>进行测试:

static const bool test = has_helloworld<Hello<field_type>>::value;

Demo