C++11 static assert for equality comparable type?

匿名 (未验证) 提交于 2019-12-03 02:01:02

问题:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!