c++name-lookupglobal-scopequalified-namescope-resolution-operator

Is a fully qualified class name down to global scope ever required for out-of-line member function definitions?


This question got me wondering whether it is ever useful/necessary to fully qualify class names (including the global scope operator) in an out-of-class member function definition.

On the one hand, I've never seen this done before (and the syntax to properly do so seems obscure). On the other, C++ name lookup is very non-trivial, so maybe a corner case exists.

Question:

Is there ever a case where introducing an out-of-class member function definition by
ReturnType (::Fully::Qualified::Class::Name::MemberFunctionName)(...) { ... }
would differ from
ReturnType Fully::Qualified::Class::Name::MemberFunctionName(...) { ... } (no global scope :: prefix)?

Note that member function definitions must be put into a namespace enclosing the class, so this is not a valid example.


Solution

  • A using-directive can cause Fully to be ambiguous without qualification.

    namespace Foo {
        struct X {
        };
    }
    
    using namespace Foo;
    struct X {
        void c();
    };
    
    void X::c() { } // ambiguous
    void ::X::c() { } // OK