#include <iostream>
/ ****我很困惑在这里使用sfinae方法****** /
template <typename T>
struct hasTypeFoo {
//..
static constexpr bool value = true;
};
///////////////////////////////////////////////// /
struct A {
using Foo = int;
};
struct B {
};
int main()
{
constexpr bool b1 = hasTypeFoo<A>::value;
constexpr bool b2 = hasTypeFoo<B>::value;
std::cout << b1 << b2;
}
使用std::void_t
:
std::void_t
您可以通过部分专业化来实现。例如
template<typename T, typename = void>
struct hasTypeFoo : std::false_type { };
template<typename T>
struct hasTypeFoo<T, std::void_t<typename T::Foo>> : std::true_type { };
template <typename T, typename = void>
struct hasTypeFoo {
//..
static constexpr bool value = false;
};
template <typename T>
struct hasTypeFoo<T, std::void_t<T::Foo>> {
//..
static constexpr bool value = true;
};