寻找 "is_comparable" 类型特征
Looking for "is_comparable" typetrait
我正在寻找 "is_comparable" 字体特征,但找不到。
很容易构建一个检查是否为 class 实现了 operator==
,但这不包括全局定义的运算符。
是否无法实现 is_comparable 排版?
我认为你的意思是一种特征,对于两种类型 L
和 R
和
这些类型的对象 lhs
和 rhs
分别会产生 true
如果
lhs == rhs
会编译,false
否则。你很感激
理论上 lhs == rhs
即使 rhs == lhs
或 lhs != rhs
也可以编译
没有。
在那种情况下,您可以实现如下特征:
#include <type_traits>
template<class ...> using void_t = void;
template<typename L, typename R, class = void>
struct is_comparable : std::false_type {};
template<typename L, typename R>
using comparability = decltype(std::declval<L>() == std::declval<R>());
template<typename L, typename R>
struct is_comparable<L,R,void_t<comparability<L,R>>> : std::true_type{};
这应用了流行的 SFINAE 模式来定义特征
在对 this question
的回答中
一些插图:
struct noncomparable{};
struct comparable_right
{
bool operator==(comparable_right const & other) const {
return true;
}
};
struct any_comparable_right
{
template<typename T>
bool operator==(T && other) const {
return false;
}
};
bool operator==(noncomparable const & lhs, int i) {
return true;
}
#include <string>
static_assert(is_comparable<comparable_right,comparable_right>::value,"");
static_assert(!is_comparable<noncomparable,noncomparable>::value,"");
static_assert(!is_comparable<noncomparable,any_comparable_right>::value,"");
static_assert(is_comparable<any_comparable_right,noncomparable>::value,"");
static_assert(is_comparable<noncomparable,int>::value,"");
static_assert(!is_comparable<int,noncomparable>::value,"");
static_assert(is_comparable<char *,std::string>::value,"");
static_assert(!is_comparable<char const *,char>::value,"");
static_assert(is_comparable<double,char>::value,"");
如果你想让特征要求平等是对称的,不平等
也存在并且是对称的你可以自己看怎么细化
我正在寻找 "is_comparable" 字体特征,但找不到。
很容易构建一个检查是否为 class 实现了 operator==
,但这不包括全局定义的运算符。
是否无法实现 is_comparable 排版?
我认为你的意思是一种特征,对于两种类型 L
和 R
和
这些类型的对象 lhs
和 rhs
分别会产生 true
如果
lhs == rhs
会编译,false
否则。你很感激
理论上 lhs == rhs
即使 rhs == lhs
或 lhs != rhs
也可以编译
没有。
在那种情况下,您可以实现如下特征:
#include <type_traits>
template<class ...> using void_t = void;
template<typename L, typename R, class = void>
struct is_comparable : std::false_type {};
template<typename L, typename R>
using comparability = decltype(std::declval<L>() == std::declval<R>());
template<typename L, typename R>
struct is_comparable<L,R,void_t<comparability<L,R>>> : std::true_type{};
这应用了流行的 SFINAE 模式来定义特征 在对 this question
的回答中一些插图:
struct noncomparable{};
struct comparable_right
{
bool operator==(comparable_right const & other) const {
return true;
}
};
struct any_comparable_right
{
template<typename T>
bool operator==(T && other) const {
return false;
}
};
bool operator==(noncomparable const & lhs, int i) {
return true;
}
#include <string>
static_assert(is_comparable<comparable_right,comparable_right>::value,"");
static_assert(!is_comparable<noncomparable,noncomparable>::value,"");
static_assert(!is_comparable<noncomparable,any_comparable_right>::value,"");
static_assert(is_comparable<any_comparable_right,noncomparable>::value,"");
static_assert(is_comparable<noncomparable,int>::value,"");
static_assert(!is_comparable<int,noncomparable>::value,"");
static_assert(is_comparable<char *,std::string>::value,"");
static_assert(!is_comparable<char const *,char>::value,"");
static_assert(is_comparable<double,char>::value,"");
如果你想让特征要求平等是对称的,不平等 也存在并且是对称的你可以自己看怎么细化