如何检查这些结构是否具有称为“ Foo”的特定类型?

问题描述 投票:0回答:2
#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;
}
c++ templates sfinae
2个回答
0
投票

使用std::void_t

std::void_t

0
投票

您可以通过部分专业化来实现。例如

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; };

© www.soinside.com 2019 - 2024. All rights reserved.