【问题标题】:Ambiguous name lookup with using-directive使用 using 指令查找不明确的名称
【发布时间】:2015-04-26 21:48:15
【问题描述】:

不允许将命名空间和同名的类放在一个声明区域中,即

namespace A {}
class A{};

is ill-formed(见 §3.3.1/4)。但是,可以通过 using-directive 引入任一名称:

namespace N { namespace A {int i;} }

struct A {static int i;};

using namespace N;

int i = A::i; // The global struct, or namespace N::A?

这段代码格式不正确吗? VC++thinks so,还有Clang

main.cpp:7:9: error: reference to 'A' is ambiguous
int i = A::i;
        ^
main.cpp:3:8: note: candidate found by name lookup is 'A'
struct A {static int i;};
       ^
main.cpp:1:25: note: candidate found by name lookup is 'N::A'
namespace N { namespace A {int i;} }
                        ^

但是,GCC accepts it

谁是对的?

【问题讨论】:

  • GCC 选择 N::A::i。想知道为什么。
  • @Barry 我也很困惑,因为我希望它更喜欢更肤浅的声明,即“更接近”查找点。

标签: c++ language-lawyer name-lookup qualified-name


【解决方案1】:

代码格式不正确。查找 A 时,第 7.3.4/6 条步入:

如果名称查找在两个不同的名称中找到名称声明 命名空间,并且声明不声明相同的实体并且做 不声明函数,名称的使用格式不正确。

这里,命名空间是全局命名空间和N,实体是命名空间N::A和类::A

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    相关资源
    最近更新 更多