C ++ 11静态断言是否等于可比较类型? [英] C++11 static assert for equality comparable type?
本文介绍了C ++ 11静态断言是否等于可比较类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C ++ 11中,如何使用EqualityComparable概念来模板类型为 static_assert
?
How to static_assert
a template type is EqualityComparable concept in C++11?
推荐答案
您可以使用以下类型特征:
You could use the following type trait:
#include <type_traits>
template<typename T, typename = void>
struct is_equality_comparable : std::false_type
{ };
template<typename T>
struct is_equality_comparable<T,
typename std::enable_if<
true,
decltype(std::declval<T&>() == std::declval<T&>(), (void)0)
>::type
> : std::true_type
{
};
您将以这种方式进行测试:
Which you would test this way:
struct X { };
struct Y { };
bool operator == (X const&, X const&) { return true; }
int main()
{
static_assert(is_equality_comparable<int>::value, "!"); // Does not fire
static_assert(is_equality_comparable<X>::value, "!"); // Does not fire
static_assert(is_equality_comparable<Y>::value, "!"); // Fires!
}
这是 实时示例 。
这篇关于C ++ 11静态断言是否等于可比较类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文