gpt4 book ai didi

c++ - 函数局部枚举声明和 ADL 的交互

转载 作者:太空狗 更新时间:2023-10-29 20:51:05 26 4
gpt4 key购买 nike

命名空间 X 中的函数通过自动推导的返回类型从本地定义的枚举类返回一个枚举器。然后将该返回值传递给一个不合格的函数,并且在 g++(7.3、8.2、主干)上,它在 namespace X 中找到该函数。在 clang 上,它给出了一个找不到函数的错误。

我假设它与 ADL 以及函数局部声明的枚举是否在函数的包含命名空间中有关。

哪个编译器是正确的,为什么? (赞赏标准引用。)

namespace X {

template <typename EnumT>
EnumT getA(EnumT) {
return EnumT::A;
}

enum class Foo { B, A, C };

auto getLocalEnumerator() {
enum class Bar { A, B, C };
return Bar::C;
}
}

int main() {
auto e1 = X::Foo::C; // unambiguously in namespace X
auto e2 = X::getLocalEnumerator(); // unsure what namespace this is

auto a1 = getA(e1); // obvious use of ADL
auto a2 = getA(e2); // clang: error, g++: ADL ok
}

在 godbolt 上直播:https://godbolt.org/g/w2DhDm

谢谢!

最佳答案

这是一个 clang 错误。来自 [basic.lookup.argdep]/2 :

The sets of namespaces and classes are determined in the following way: [...] If T is an enumeration type, its associated namespace is the innermost enclosing namespace of its declaration. If it is a class member, its associated class is the member's class; else it has no associated class.

本地枚举 Bar 将最内层的封闭命名空间...作为其关联的命名空间,即 X。对 getA 的非限定查找未发现任何内容,因此我们继续查看所有参数的所有关联命名空间 - 关联命名空间的集合是 {X}。在那里查找应该找到 X::getA

关于c++ - 函数局部枚举声明和 ADL 的交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51867524/

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