Difference between `using` and `typedef`

D

drrngrvy

Hi all,

Consider:

<>
namespace a {
namespace nested { class a_class {}; }
}
namespace b { using ::a::nested::a_class; }
namespace c { typedef ::a::nested::a_class a_class; }
</>

Is there any difference between b::a_class and c::a_class? I have a
feeling that b::a_class uses ADL, whereas c::a_class doesn't, but I'm
neither sure why [I think it] nor trust I have the right impression.

Are there any situations where there's a difference between the two?
 
V

Victor Bazarov

drrngrvy said:
Consider:

<>
namespace a {
namespace nested { class a_class {}; }
}
namespace b { using ::a::nested::a_class; }
namespace c { typedef ::a::nested::a_class a_class; }
</>

Is there any difference between b::a_class and c::a_class? I have a
feeling that b::a_class uses ADL, whereas c::a_class doesn't, but I'm
neither sure why [I think it] nor trust I have the right impression.

Are there any situations where there's a difference between the two?

You need to find the places where a typedef-name is unacceptable.
I can recall only a couple at this time: elaborate type specifiers,
definition of a constructor or destructor (a type name is needed).
Since it is unlikely you're going to define the c-tor or d-tor in
the other namespace, the only real "danger" is to use the name
'a_class' to declare it a friend of something in 'b' or 'c', which
requires an elaborate type specifier, where a typedef-name is not
allowed.

V
 

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,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top