"Stephen Horne" <
[email protected]>
Why not do it the suggested way:
using std::cout;
using std::endl;
Well, you can't provide a new name -- but you didn't want one in the
example, and it's a usual situation.
1. The new name thing is directly relevant to the unordered_map
rather than hashmap issue I was discussing. Remember, the clashes
they are trying to avoid are with pre-existing non-standard
libraries. Someone wanting to mix hashmap implementations (e.g.
during a transition from old in-house libraries to new standard
ones) may well want an alias.
2. A single uniform alias construct would be convenient anyway,
irrespective of namespaces. I get annoyed when I just want an
alias for something, and need to figure out what it is in order to
choose the right way to alias it.
For example, is std::endl a constant? A function? What type is it?
Obviously, I never actually alias it, but these are questions I'd
have to answer if I ever did. No big deal - but still a
distraction when all you want is an alias. In fact, rather than
answer those questions, a lot of people would probably take the
easy (in the short term) path - #define. It doesn't seem such a
bad idea - until you use Doxygen, or ...
std::endl might seem exceptional in that the user doesn't
necessary know the type of the thing he's using, but that isn't so
rare as it might seem, especially WRT templates. For example...
template<typename T> void blah<T> ()
{
// what is the type of T::some_member?
}
Also, how exactly do you alias a variable without causing a
run-time overhead? What I tend to do is use a reference
variable...
thing_t& shortname (longname);
But in doing that, I'm risking a significant run-time overhead.
Often I don't care (not an inner loop etc), and very likely the
optimiser can eliminate that overhead anyway, but it's still a
potential issue that IMO should never exist.
3. I'd forgotten that I could use 'using' that way. Personally, I
tend to have a lot of "std::" in my code. And "sh::", for my own
libraries. Occasionally, I'll put "using namespace" in a local
scope somewhere, but I only ever put it at the global level in
trivial (usually regression test) programs.