2

I have a large software framework which is currently living in a common namespace. Recently, I've moved some classes into nested namespaces, but in order to preserve backwards compatibility for the time being, I need to keep the names in the global namespace. So far, I'm using using:

namespace framework {
  namespace IO {
    struct IStream;
  }
#if COMPATIBILITY
using IO::IStream;
#endif 
}

However, I could equally well use typedef IO::IStream IStream;. Is there some advantage/disadvantage of using typedef over using?

Anteru
  • 19,042
  • 12
  • 77
  • 121
  • but beware of ADL with _'used'_ function overloads; using for non-types might not exactly do what you think (http://stackoverflow.com/questions/2953684/why-doesnt-adl-find-function-templates) – sehe Oct 05 '11 at 11:53
  • possible duplicate of [What are the differences between typedef and using?](http://stackoverflow.com/questions/7657710/what-are-the-differences-between-typedef-and-using) – Steve Jessop Oct 05 '11 at 12:01

1 Answers1

4

They're somewhat different things: The typedef introduces a new type name framework::IStream, whereas the using directive only affects the name lookup inside the scope in which it appears. (This has additional effects if you were to also define a separate, genuine type framework::IStream, but since you're not doing that, this isn't an issue.)

In that sense I'd say that using is an implementation detail, which is preferable over a global change of semantics that would come from introducing a new type name. So if you can get away with it, use the using directive in those scopes where it's needed, and you can gradually migrate those to the new system.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • `typedef` does not create new types. It creates aliases and compiler does not make any difference between the "source" and "destination" typed in typedef. – n0rd Oct 05 '11 at 11:32
  • 2
    @n0rd: I changed it to "type name". – Kerrek SB Oct 05 '11 at 11:34