gpt4 book ai didi

c++ - std::optional 枚举的比较运算符

转载 作者:太空宇宙 更新时间:2023-11-03 10:38:25 25 4
gpt4 key购买 nike

根据 this , 在 optional<T> 上使用比较运算符和 optional<U>如果为基础类型定义了相同的运算符,应该可以工作 TU .

我正在尝试使用在不同命名空间中定义的两个枚举(实时代码 here)来尝试以下示例,但无法弄清楚编译失败的原因:

#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);
}

我的 GCC-8.2.0 是这样说的:

invalid operands to binary expression ('const std::optional<n1::tag>' and 'const std::optional<n2::tag>')

有什么想法吗?我发现将每个枚举移出它们的命名空间,一切都按预期工作(如 here )。

最佳答案

<运算符必须位于其参数的任何关联命名空间中,即它必须位于任一命名空间 n1 中或 n2但自 n2::tagn1::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);
}
}

关于c++ - std::optional 枚举的比较运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52917842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com