我正在处理概念,并期望std::is_equality_comparable为向量工作,但它不工作。
#include <type_traits>
#include <vector>
#include <concepts>
struct X {};
template<std::equality_comparable T>
bool foo( T v){
return v == v;
}
int main()
{
foo(10);
foo(std::vector<X>{});
}编译错误在foo内部失败,而不是在受概念保护的函数边界处失败。

这是一种错误还是预期的行为?
发布于 2021-03-12 11:44:21
概念只检查声明是否格式良好,而不检查定义。
比较算子 for std::vector并不是SFINAE友好的,即它们是无条件声明的,这意味着operator==(vector<T>, vector<T>)始终存在,即使operator==(T, T)不存在。这就是为什么equality_comparable<std::vector<T>>总是满意的原因,并且在函数中得到了v == v上的错误。
为了使其正常工作,应当限制向量比较运算符,即:
template< class T, class Alloc >
requires std::equality_comparable<T>
constexpr ret_type operator==( const std::vector<T,Alloc>& lhs,
const std::vector<T,Alloc>& rhs );https://stackoverflow.com/questions/66598738
复制相似问题