Я видел этот вопрос , который позволяет проверить существование функции-члена , но я пытаюсь выяснить, имеет ли класс тип члена . ].
В приведенном ниже примере оба оцениваются как «false», но я хотел бы найти способ, чтобы has_bar
оценивалось как false, а has_bar
оценивалось как 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;
}