Does it actually happen?

http://ideone.com/T0egOt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
 
namespace ns //::ns
{
    void f() //::ns::f
    {
        std::cout << "::ns::f"<< std::endl;
    }
    namespace ns //::ns::ns
    {
        void f() //::ns::ns::f
        {
            std::cout << "::ns::ns::f"<< std::endl;
        }
    }
}
 
using namespace ns; //::ns
 
int main() //::main
{
    ::ns::f(); //::ns::f
    ::ns::ns::f(); //::ns::ns::f
    f(); //::ns::f
    ns::f(); //error, ambiguous
}
Obviously this is another reason that "using namespace <something>;" is bad, but someone I showed this to told me it never actually happens in real code that you have something with the same name as the thing it is inside of (besides constructors/destructors).

They even ignored me when I said a namespace may have a class in it that has the same name, which (I thought) happens quite often in programming. Who do you think is right?
Last edited on
I don't think it's too much of a a stretch: one of the classes I'm using a lot is named the same way as the (4th-level nested) namespace it's in, except for a minor capitalization difference. And yes, the naming scheme actually makes sense (and no, I wasn't the person who named it). I don't think it's "quite often" though.
Last edited on
closed account (zb0S216C)
LB wrote:
"I showed this to told me it never actually happens in real code that you have something with the same name as the thing it is inside of (besides constructors/destructors)."

...but you've shown him/her an example of it. Real-world code is no different than code that one has written for their personal use. If I wrote an array wrapper for a project at work, it would functional exactly the same if I used it in a personal project.

The fact is, it will only happen if the program/library is poorly structured or if the naming convention is appalling. Programmers who use "using namespace ..." excessively are likely to trigger such ambiguities.

Wazzak
Last edited on
Topic archived. No new replies allowed.