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 { };
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 * .
?
source share