Template has_type returns true for type struct {};

There are several ways to implement the has_type<T> template, which outputs if T has a nested class or typedef named type . i.e

 namespace detail { template<typename> struct tovoid { typedef void type; }; } template<typename T, typename = void> struct has_type : std::false_type { }; // this one will only be selected if C::type is valid template<typename C> struct has_type<C, typename detail::tovoid<typename C::type>::type> : std::true_type { }; 

or

 template <typename C> char test_for_type(...) { return '0'; } template <typename C> double test_for_type(typename C::type const *) { return 0.0; } template <typename T> struct has_type { static const bool value = sizeof(test_for_type<T>(0)) == sizeof(double); }; 

however, in any case, has_type<type>::value is true for this class:

 struct type { }; 

Now the above type does not have another type nested inside it, but it does have a constructor of type::type() .

But should this constructor “run” checks for a nested type? Or is this a compiler error? (I would like to think that typename type::type not applicable to the constructor and / or that you could not take a pointer to the constructor, for example, what would be created by the second testing method: typename type::type const * .

?

+5
source share
1 answer

The class name is "injected" into the scope of the class, so type::type really a type name, and it has the same type as ::type .

+5
source

Source: https://habr.com/ru/post/1201651/


All Articles