mardi 25 avril 2023

How can I access a C++ function if an inline namespace has the same function?

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.

Aucun commentaire:

Enregistrer un commentaire