5

The following situation:

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!
    }
}

GCC and clang agree, here is the GCC error message:

<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

I can explicitly specify the inline namespace to access the overload there, but what about the abc::f() version? I can't find a syntactical way to access it. Is there really no way to do this?

I know the question is not very relevant to practice. Nevertheless, I find it interesting.

Jason
  • 36,170
  • 5
  • 26
  • 60
Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
  • 5
    I guess not at all? Not solving your problem but here is a similar issue: `int i = 0; { int i = 1; }` Afaik there is no way to access the outer `i` in the inner scope. – chrysante Apr 25 '23 at 22:42
  • For that page https://learn.microsoft.com/en-us/cpp/cpp/namespaces-cpp?view=msvc-170, I would expect that a given function exist only in one parent or inline namespace. Doing otherwise, you go against the purpose of the tool. As far as I understand an inline namespace is essentially equivalent to a using namespace declaration from the parent and would give the same conflicts... – Phil1970 Apr 26 '23 at 00:02
  • You can disambiguate them by explicitly providing their sub-namespace, like `x::f();` or `y::f();`. – Eljay Apr 26 '23 at 01:33

1 Answers1

3

You don't. Symbols in inline namespaces are fully part of their enclosing namespace as well as part of their inline namespace.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524