2021年3月7日星期日

Is there any way to check if a template class has a nested class definition (or a typedef) in C++14?

I have the following structures defined:

struct NoRelationship {};  struct OneToMany      {};  struct Interdependent {};    template <typename RelationType>  struct SignatureGenerator  {      using type = std::function<void()>;  };    template <>  struct SignatureGenerator<Interdependent>  {      using type = std::function<void()>;      using extra_type = std::function<void()>;  };  

I need to implement a SFINAE check which would determine if a class has the "extra_type" definition. I have checked this and this topics, but still can't find a working solution. I have tried the following code from the latter topic:

template <typename T>  class has_extra_type  {      typedef char yes;      struct no {char x[2];};      template<typename C> static yes test(decltype(&C::extra_type));      template<typename C> static no test(...);  public:      enum { value = sizeof(test<T>(0)) == sizeof (char)};  };  

But this doesn't work, because of the 'missing 'typename' prior to dependent type name' error. That is what the compiler tells if I comment out the general specification:

//    template<typename C> static no test(...);  

error: no matching function for call to 'test' enum { value = sizeof(test(0)) == sizeof (char)};

note: candidate template ignored: substitution failure [with C = SignatureGenerator]: missing 'typename' prior to dependent type name 'SignatureGenerator::extra_type' template static yes test(decltype(&C::extra_type));

Any ideas on how to bypass this?

https://stackoverflow.com/questions/66523483/is-there-any-way-to-check-if-a-template-class-has-a-nested-class-definition-or March 08, 2021 at 10:07AM

没有评论:

发表评论