c++ - Comparison operator for std::optional enum
问题描述
According to this, using a comparison operator on an optional<T>
and optional<U>
should work provided that the same operator is defined for the underlying types T
and U
.
I am trying out the following example with two enums defined in different namespaces (live code here) and can't figure out why it fails to compile:
#include <optional>
namespace n1
{
enum class tag : unsigned {I,II,III};
}
namespace n2
{
enum class tag : unsigned {I,II,III};
}
bool operator<(const n1::tag& t1, const n2::tag& t2)
{
return static_cast<unsigned>(t1) < static_cast<unsigned>(t2);
}
int main()
{
const std::optional<n1::tag> o1(n1::tag::I);
const std::optional<n2::tag> o2(n2::tag::I);
bool t = (o1 < o2);
}
My GCC-8.2.0 says that:
invalid operands to binary expression ('const std::optional<n1::tag>' and 'const std::optional<n2::tag>')
Any ideas? I've found out that moving each enum out of their namespaces, things work as expected (as here).
解决方案
运算符必须在它参数的<
任何关联命名空间中,即它必须在命名空间中,n1
或者n2
但由于n2::tag
在定义时不可见,因此n1::tag
您需要将运算符放置在命名空间中n2
或重新打开命名空间n1
。
在命名空间中定义运算符n2
:
namespace n2
{
enum class tag : unsigned {I,II,III};
bool operator<(const n1::tag& t1, const n2::tag& t2)
{
return static_cast<unsigned>(t1) < static_cast<unsigned>(t2);
}
}
打开命名空间n1
:
...
namespace n2
{
enum class tag : unsigned {I,II,III};
}
namespace n1 {
bool operator<(const n1::tag& t1, const n2::tag& t2)
{
return static_cast<unsigned>(t1) < static_cast<unsigned>(t2);
}
}
推荐阅读
- python - 如何使 Python 运行时错误更详细?
- javascript - 如何编写一个函数,通过返回布尔值来检查数组是否包含数字
- javascript - 数组没有更新值
- mysql - MySQL:返回 NULL 或 0 Vales 的计数 (*)
- php - 如何通过使用斜线迁移来更新数据库中的数据?
- javascript - 如何忽略Javascript中的负数?
- node.js - 使用 AJAX 填充 HTML 表
- c# - 自引用循环,而我的持久天蓝色函数中有两个活动函数
- google-app-engine - 如何通过云外壳访问应用程序引擎中的系统代码?
- python - 分析包含 Dask 并行化的 Python