tags:

views:

491

answers:

2

I do not like using namespace std, but I am also tired of having to type std:: in front of every cout, cin, cerr and endl. So, I thought of giving them shorter new names like this:

// STLWrapper.h

#include <iostream>
#include <string>

extern std::ostream& Cout;
extern std::ostream& Cerr;
extern std::istream& Cin;
extern std::string&  Endl;

// STLWrapper.cpp

#include "STLWrapper.h"

std::ostream& Cout = std::cout;
std::ostream& Cerr = std::cerr;
std::istream& Cerr = std::cin;
std::string _EndlStr("\n");
std::string& Endl = _EndlStr;

This works. But, are there any problems in the above which I am missing? Is there a better way to achieve the same?

+19  A: 

Why not

using std::cin;
using std::cout;

and so on? Then in your code you can use cin, cout, and so on, without accidentally injecting all of the rest of the std namespace into your code.

Alex Martelli
[hand to head] I was not even aware that "using" can be used with non-namespace entities! Thanks! :-)
Ashwin
@Ashwin: `using namespace_name::identifier` is called a "namespace declaration", whereas `using namespace_name` is called a "namespace directive".
sbi
Sbi: Thanks so much for that! I am now going back to the books to read all about "using".
Ashwin
@Ashwin: Once you're reading up `using`, it can also be used to bring a base class identifier into a derived class' scope, so that it is considered for overload resolution, or to make a private base's identifier accessible in a derived class' interface. However, I haven't really seen those in the wild.
sbi
+21  A: 

Alex has given you an answer how to syntactically solve that problem. However, I want to point out two other arguments regarding this issue:

  1. No matter whether you're employing a using directive (using namespace std) or its lesser evil sister, a using declaration (using std::cout), overloading might lead to nasty surprises. It's not much hassle to type std:: compared to spending half a night debugging to find out your code called std::distance() instead of your own distance() function, just because you made a small mistake and std::distance() accidentally is a better match.

  2. A line of code gets written once, but - depending on its lifetime - it is read tens, hundreds, and some even thousands of times. So the time it takes to write a line of code simply doesn't matter at all, important is only the time it takes to read and interpret a line of code. Even if it takes three times as long to write a line with all the proper std:: in place, if it makes reading it only 10% faster, it is still worth the trouble.
    So the important question is: Is it easier to read and interpret a line of code with all the std:: in place or is it harder? From another answer:

    Here's one more data point: Many, many years ago, I also used to find it annoying having to prefix everything from the standard library with std::. Then I worked in a project where it was decided at the start that both using directives and declarations are banned except for function scopes. Guess what? It took most of us very few weeks to get to used to write the prefix and after a few more weeks most of us even agreed that it actually made the code more readable. (There's a reason for that: Whether you like shorter or longer prose is subjective, but the prefixes objectively add clarity to the code. Not only the compiler, but you, too, find it easier to see which identifier is referred to.)

    In almost a decade, that project grew to have several million lines of code. Since these discussions comes up again and again, I once was curious how often the (allowed) function-scope using actually was used in the project. I grep'd the sources for it and only found one or two dozen places where it was used. To me this indicates that, once tried, developers didn't find std:: painful enough to employ using directives even once every 100kLoC, even where it was allowed to be used.

    I think it's sad that every book and tutorial you'll find skips std::, because that makes people getting used to read the code that way. When I taught C++ for several years (after the above mentioned experience), I told my students that I don't want to see any using directive or declaration in their code. (The only exception to that rule is using std::swap, BTW, which you'll need in order to have swap(a,b) pick up overloads outside of namespace std.) Once they got used to it, they didn't mind and, when asked about it, they said they find code without the std:: prefix confusing. Some even added the std:: prefix to code they typed from a book or tutorial which didn't have it.

Bottom line: What's so hard about typing std:: that everybody gets so worked up about it? By now I have been doing it for >15 years, and I don't miss using at all.

sbi
Ashwin
+1 (I wish I could do more) for "A line of code gets written once, but - depending on its lifetime, it is read tens, hundreds, and some even thousands of times. So the time it takes to write a line of code simply doesn't matter at all, important is only the time it takes to read a line and interpret of code." -- So True but so difficult to convince this to coworkers.
ArunSaha
+1 Great argument. Count me as another convert. I'm adding 'using' to my list of verboten mechanisms along with arrays, global variables, macros, if-then-elseif-elseif-elseif... monstrosities, etc.
Amardeep
Hum, didn't see that. I am also convinced that prefixing adds clarity, especially when namespaces and subfolders match, so that you know which include brought that particular object :)
Matthieu M.