C++ Standard Library: How to write wrappers for cout, cerr, cin and endl?

c++, iostream

Solution

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.

Problem

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?

Original source

Related problems