This question already has answers here:
How to write `is_complete` template?
(7个答案)
7年前关闭。
是否可以使用SFINAE检查类型是否已完全定义?
例如。
解决方案不应修改哈希结构。
并通过确定
正如丹尼尔(Daniel)在回答中所说,这种事情的效用是有限的。特质实际上并不测试在您查询的代码中该类型是否完整,它会在程序中首次为给定类型实例化该特质的位置上测试该类型是否完整。
(7个答案)
7年前关闭。
是否可以使用SFINAE检查类型是否已完全定义?
例如。
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");
解决方案不应修改哈希结构。
最佳答案
您可以使用is_complete
类型特征,因为它的格式不正确,无法对sizeof(T)
类型的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 {};
并通过确定
is_defined_hash_type<T>
是否完整来使用它来检查hash<T>
。 (Live at Coliru)正如丹尼尔(Daniel)在回答中所说,这种事情的效用是有限的。特质实际上并不测试在您查询的代码中该类型是否完整,它会在程序中首次为给定类型实例化该特质的位置上测试该类型是否完整。
关于c++ - 使用SFINAE检查类型是否完整,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21119281/