'using namespace std;'


for all the time I’ve been coding I have always used the line ‘using namespace std;’ at the beginning. I have noticed a lot of criticism against it.

Can I ask why this line of code is frowned upon?

In a console program can you mix seperate classes, some in namespace std and others without it?

And seeing as it considered a bad approach, what is the worst it could do to a simple 2D indie game?

I’m not an c++ expert, but I do coding since 1999 (as my profession). So I try to answer, what I learned about it.

C++ uses namespaces to avoid name clashes. It can be compared to Java and the packages in there. That’s good, because so you can write a function bestFunctionEver (in namespace foo) and I can create a library with bestFunctionEver (in namespace bar). You can use both in you code, with just writing foo::bestFunctionEver or bar::bestFunctionEver.

The command using namespace std will remove the namespace for the std. Consider you are writing your code in foo and have a string class. Because you are within your own namespace, you can call string. It’s fine. Now you set using namespace std. The compiler didn’t know which string class do you want, so you have to write foo::string or std::string.

Your code will compile as normal, but in my opinion it’s better to avoid using namespace for external libraries and to prefix the calls.

1 Like

For me I just prefix everything with its namespace for my own sanity, others and then the compiler last :slight_smile: lol




It’s only bad if you put the using namespace in your header files, which might be used by any of the implementation files. Putting it at the top of your implementation file is fine, however for the last few years I have not being doing this for std and it seems natural to add std:: for every reference to the STL types.


I use using namespace std so I don’t have to write out std:: every time just a lazy coder :slight_smile: My approach is to keep things simple and easy to code. Stuff like sharing implementation and such I will cross that bridge when it happens. Just keep them back of my mind it might happen later or not.

is Bad

using namespace std;
using namespace boost;

vector a; //ERROR Bad!!!

is Good

std::vector a; //Good
boost::vector b; //Good

“using” - a powerful thing

#if defined (RenderingMap)
using TileMap_t = cocos2d::experemental::TMXTileMap;
using TileMap_t = cocos2d::TMXTileMap;

auto map = TileMap_t::create() //GOOD
using ObjectMap = std::unorder_map<MyEnum, cocos2d::Node*>;

ObjectMap objMap= {/*init*/};
auto obj = objMap[MyEnum::Type_0];

use alias

using StlVector = std::vector;
using MyVector = My::vector;

StlVector a;
MyVectpr b;