namspace: Why behaviour is like this?

D

doublemaster007

namespace A {class AA {};}
namespace B {class A A{};}
namespace C {using namespace A; using namesace B} /why no error: no
ambiguity?
 
M

Muhammed

namespace A {class AA {};}
namespace B {class A A{};}
namespace C {using namespace A; using namesace B} /why no error: no
ambiguity?

sorry..
namespace A {class AA {};}
namespace B {class AA {};}
namespace C {using namespace A; using namesace B}
 
L

litb

namespace A {class AA {};}
namespace B {class A A{};}
namespace C {using namespace A; using namesace B} /why no error: no
ambiguity?

Neither of them declare anything. Rather, they make names visible when
looked up as-if they were declared in the nearest enclosing namespace
that contains both the using directive and the destination namespace.
Would be the global namespace here. The using directive never results
in a direct error.
 
M

Muhammed

Neither of them declare anything. Rather, they make names visible when
looked up as-if they were declared in the nearest enclosing namespace
that contains both the using directive and the destination namespace.
Would be the global namespace here. The using directive never results
in a direct error.

hmm.thank you.. sorry that i am not very convinced.

still

namespace A {class AA {};}
namespace B {typedef A::AA AA;} /doesnt give error here
namespace C {using namespace A; using namesace B} // no error


but if say C::AA compiler barks. I am really confused with these
behaviours.
 
B

Bo Persson

Muhammed said:
hmm.thank you.. sorry that i am not very convinced.

still

namespace A {class AA {};}
namespace B {typedef A::AA AA;} /doesnt give error here
namespace C {using namespace A; using namesace B} // no error


but if say C::AA compiler barks. I am really confused with these
behaviours.

The name C::AA can mean either A::AA or B::AA, so its use is
ambiguous. But if you don't use it, who cares?


Bo Persson
 
L

Lars Tetzlaff

Muhammed said:
hmm.thank you.. sorry that i am not very convinced.

still

namespace A {class AA {};}
namespace B {typedef A::AA AA;} /doesnt give error here
namespace C {using namespace A; using namesace B} // no error


but if say C::AA compiler barks. I am really confused with these
behaviours.

Why do you expect an error? The using directive says: iff you search for
a type, consider all types in this namespace too. Not more. So the
following code is legal:

namespace A {class AA {};}
namespace B {typedef A::AA AA;} //doesnt give error here
namespace C {using namespace A; using namespace B; A::AA a; B::AA b; }
// no error


But what AA should be used if you say C::AA?

You could use

namespace A {class AA {};}
namespace B {typedef A::AA AA;} //doesnt give error here
namespace C {using namespace A; using namespace B; typedef A::AA AA; }
// no error

C::AA x;


Lars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,428
Latest member
RosalieQui

Latest Threads

Top