【发布时间】:2022-01-11 04:09:39
【问题描述】:
我试图在编译时测试两种类型是否可以相等,并且我已经为它们定义了operator==,所以它们应该是。但是,以下代码无法编译:
#include <string_view>
struct A { int n; };
bool operator==(const A& a, const std::string_view s) { return a.n == s.size(); }
static_assert(std::equality_comparable_with<A, std::string_view>);
(godbolt)
我什至尝试将operator== 定义为相反的顺序,并将A 定义为自身,但它也不起作用。
当然,像下面这样就可以了
static_assert(std::equality_comparable_with<std::string, std::string_view>);
我在这里错过了什么?
【问题讨论】:
标签: c++ operator-overloading c++20 c++-concepts