Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 730
Max Langhof Avatar asked Nov 18 '19 12:11

Max Langhof


1 Answers

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
like image 80
T.C. Avatar answered Oct 22 '22 16:10

T.C.