如果内联命名空间具有相同的函数,如何访问 C++ 函数?

Ben*_*uch 5 c++ namespaces c++11 inline-namespaces

以下情况:

namespace abc{
    inline namespace x{
        int f() { return 5; }
    }

    inline namespace y{
        int f() { return 6; }
    }

    int f() { return 7; }

    void g(){
        x::f();   // okay
        y::f();   // okay

        f();      // error: ambiguous!
        abc::f(); // error: ambiguous!
    }
}
Run Code Online (Sandbox Code Playgroud)

GCC 和 clang 同意,这是 GCC 错误消息:

<source>: In function 'void abc::g()':
<source>:16:10: error: call of overloaded 'f()' is ambiguous
   16 |         f();      // error: ambiguous!
      |         ~^~
<source>:10:9: note: candidate: 'int abc::f()'
   10 |     int f() { return 7; }
      |         ^
<source>:3:13: note: candidate: 'int abc::x::f()'
    3 |         int f() { return 5; }
      |             ^
<source>:7:13: note: candidate: 'int abc::y::f()'
    7 |         int f() { return 6; }
      |             ^
<source>:17:15: error: call of overloaded 'f()' is ambiguous
   17 |         abc::f(); // error: ambiguous!
      |         ~~~~~~^~
<source>:10:9: note: candidate: 'int abc::f()'
   10 |     int f() { return 7; }
      |         ^
<source>:7:13: note: candidate: 'int abc::y::f()'
    7 |         int f() { return 6; }
      |             ^
<source>:3:13: note: candidate: 'int abc::x::f()'
    3 |         int f() { return 5; }
      |             ^
Compiler returned: 1
Run Code Online (Sandbox Code Playgroud)

我可以显式指定inline namespace来访问那里的重载,但是abc::f()版本呢?我找不到访问它的语法方法。难道真的没有办法做到这一点吗?

我知道这个问题与实践不太相关。尽管如此,我还是觉得很有趣。

Yak*_*ont 3

你不知道。内联命名空间中的符号完全是其封闭命名空间的一部分,也是其内联命名空间的一部分。