Using SFINAE to check if the type is complete or not
c++, c++11, metaprogramming, sfinae
Solution
You can make an `is_complete` type trait, using the fact that it is ill-formed to evaluate `sizeof(T)` for an incomplete type `T`:
template <typename T>
struct is_complete_helper {
template <typename U>
static auto test(U*) -> std::integral_constant<bool, sizeof(U) == sizeof(U)>;
static auto test(...) -> std::false_type;
using type = decltype(test((T*)0));
};
template <typename T>
struct is_complete : is_complete_helper<T>::type {};
and use it to check for `is_defined_hash_type<T>` by determining if `hash<T>` is complete. (Live at Coliru)
As Daniel says in his answer, the utility of such a thing is limited. The trait doesn't actually test if the type is complete at the point in the code where you query, it tests if the type was complete at the point in the program where the trait was first instantiated for a given type.
Problem
Is it possible to check with SFINAE if the type is completely defined? E.g. ``` template <class T> struct hash; template <> struct hash<int> {}; // is_defined_hash_type definition... enum Enum { A, B, C, D }; static_assert ( is_defined_hash_type<int> ::value, "hash<int> should be defined"); static_assert (! is_defined_hash_type<Enum>::value, "hash<Enum> should not be defined"); ``` The solution should not modify the hash struct.