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?
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 typeframework::IStream, but since you’re not doing that, this isn’t an issue.)In that sense I’d say that
usingis 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 theusingdirective in those scopes where it’s needed, and you can gradually migrate those to the new system.