Question

I'd like to know if fully qualifying names in a library is necessary.

For example (I indent namespaces here for readability):

namespace A {
    namespace B {
        namespace C {
            class Foo { ... };
        } // namespace C

        ::A::B::C::Foo foo;
        // or
        C::Foo foo
    } // namespace B
    namespace D {
        ::A::B::C::Foo foo;
        // or
        B::C::Foo foo
    } // namespace D
} // namespace A

I didn't fully understand the 3.4 chapter of the C++ standard, and I saw something I can't explain in the STL vector header (simplified for readability):

namespace std {
    template<..
    class reverse_iterator
    { ... };

    template<..
    class vector {
        typedef _STD reverse_iterator<const_iterator> const_reverse_iterator;
        ...
    }
}

The thing is _STD is expanded to ::std:: .. but according to my understanding of the name look up, reverse_iterator would have been looked up for in vector first then in the parent namespace which is std. How could a collision would be possible ?

subsidiary question : what would be the name lookup rules for names in a template functions with arguments ? I think that a qualified-id would look directly for names already declared without ADL but an unqualified name would do an ADL, am I right ?

Thank you in advance for your answers.

Was it helpful?

Solution

You're right, the qualification appears to be unnecessary there.

Don't try to learn C++ from standard library code, it has to be written in a very peculiar style for various reasons that probably don't apply to you, including backwards compatibility, portability, alternative configurations (e.g. disabling all namespaces for historical compilers). It's possible that in some configurations _STD expands to something different, or it's possible that it's just redundant (but harmless.)

what would be the name lookup rules for names in a template functions with arguments ? I think that a qualified-id would look directly for names already declared without ADL but an unqualified name would do an ADL, am I right ?

Yes, ADL only applies to unqualified names.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top