Можно ли использовать свойства типа _/ SFINAE, чтобы узнать, определяет ли класс член TYPE?

Я видел этот вопрос , который позволяет проверить существование функции-члена , но я пытаюсь выяснить, имеет ли класс тип члена . ].

В приведенном ниже примере оба оцениваются как «false», но я хотел бы найти способ, чтобы has_bar::valueоценивалось как false, а has_bar::valueоценивалось как true .

Это возможно?

#include 

struct foo1;
struct foo2 { typedef int bar; };

template 
class has_bar
{
    typedef char yes;
    typedef long no;

    template  static yes check( decltype(&C::bar) ) ;
    template  static no  check(...);
public:
    enum { value = sizeof(check(0)) == sizeof(yes) };
};

int main()
{
    std::cout << has_bar::value << std::endl;
    std::cout << has_bar::value << std::endl;
    return 0;
}

Изменить :, реализуя специализацию в ответ на ответы ниже:

...if you use C::bar in the target template, the template will be discarded automatically for types that don't have that nested type.

Я пытался это сделать, но явно чего-то не хватает

#include 

struct foo1;
struct foo2 { typedef int bar; };

template 
struct target
{
    target()
    {
        std::cout << "default target" << std::endl;
    }
};

template
struct target
{
    target()
    {
        std::cout << "specialized target" << std::endl;
    }
};

int main()
{
    target();
    target();
    return 0;
}

14
задан Community 23 May 2017 в 11:33
поделиться