How to static_assert
a template type is EqualityComparable concept in C++11?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You could use the following type trait:
#include template struct is_equality_comparable : std::false_type { }; template struct is_equality_comparable() == std::declval(), (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::value, "!"); // Does not fire static_assert(is_equality_comparable::value, "!"); // Does not fire static_assert(is_equality_comparable::value, "!"); // Fires! }
Here is a live example.