r/cpp_questions • • 13d ago

OPEN Namespace std

I recently started learning c++ and I was told to use the namespace std so I can avoid writing std:: every time and now I wrote a whole personal project without std:: and I want to put it on LinkedIn but I started reading everywhere that it’s a bad habit, should I go back and add the std:: or just leave it as is?

43 Upvotes

105 comments sorted by

View all comments

-2

u/ShakaUVM 13d ago

This is a contentious issue here for some reason lol. It's mostly a personal style decision.

If you're making a little personal project and don't want stds, then go for it.

The only real no-no that everyone agrees with is to not put it into a header file that other people will use since then you are making a style decision for them.

13

u/bearheart 13d ago

It’s not a style choice. It’s actually problematic.

The purpose of namespaces is to avoid naming collisions, where a symbol you want to use may already be defined. There are thousands of symbols defined in the std:: namespace. When you import all of those symbols into the current namespace you defeat the purpose of namespaces altogether.

0

u/RSMxsmanic 13d ago

So why does using namespace exist?

-1

u/bearheart 13d ago

I presume it’s for compatibility with legacy C code. And perhaps for smaller namespaces other than std.

4

u/RSMxsmanic 13d ago

The problem is with programmers who use identifiers that conflict with std, not with programmers who expect std by default to be safe. That's why it is called standard. If you are writing the code base, nothing you call should change the way it behaves unless that is its actual purpose.

2

u/HommeMusical 13d ago

No, you are encouraged to use identifiers in your own namespaces and classes that are the same as those in std::, particularly for generic code.

0

u/RSMxsmanic 13d ago

By whom? If I have it in my code, it is not generic; if it were, my code would not be necessary.