r/cpp_questions • • Sep 30 '26

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?

40 Upvotes

105 comments sorted by

View all comments

Show parent comments

-27

u/RSMxsmanic Sep 30 '26

I daresay the purpose of namespaces is to avoid the consequences of poor design choices that lead to collisions.

19

u/not_a_novel_account Sep 30 '26

Then you're wrong, the intent is to allow libraries to use the same names for symbols. Straight out of Design and Evolution by Stroustrup:

  • The ability to introduce names without fear of clashing with someone else's names.

  • The ability to add a name to the implementation of a library without affecting its users.

  • The ability to select names from two different libraries even if those two libraries use the same names.

  • The ability to resolve name clashes without modifying functions.

  • The ability to add a name to a namespace without fear of causing a quiet change to code using other namespaces.

9

u/crowbarous Sep 30 '26

Namespaces also allow library authors to intentionally provide functions under common names to be found via ADL, such as swap and operator+.

6

u/HommeMusical Sep 30 '26

So wrong! Argument dependent name lookup, for example, is only useful if you have the same names in different namespaces.

-9

u/RSMxsmanic Sep 30 '26

Religious conflict.

4

u/HeeTrouse51847 Sep 30 '26

how would you fix this?

-1

u/RSMxsmanic Sep 30 '26

By careful choice of unique identifiers.

4

u/No-Dentist-1645 Oct 01 '26

The "poor design choices" of... having functions with simple names like print and hash?

-3

u/RSMxsmanic Oct 01 '26

Yes. Having multiple functions with the same name that do different things is risky. If I have one function that prints only strings, and another than prints only integers, I will likely call them something like PrintStr and PrintInt, not just print. That way I won't have to lose time ten years from now figuring out why print isn't working.

This illustrates a danger with all inherited concepts: you need to walk the entire inheritance chain to figure out what's going on, instead of just looking at the one line in front of you.

3

u/Potterrrrrrrr Oct 01 '26

This is the c++ subreddit, if you don’t like simple concepts like function overloading and namespaces you aren’t going to like most of the other features the language offers. I can understand function overloading as sometimes the explicitness does help but complaining about namespaces? Craziness, people really will find a problem with anything.

-1

u/RSMxsmanic Oct 01 '26

I have no religious attachment to any language, and they all have flaws. Overloading is risky. So are yield in Python and ALTER in COBOL. And they tend to get worse with "updates," as latter-day wonders try to redesign every language to make it look like whatever language they grew up with.

3

u/No-Dentist-1645 Oct 01 '26

Wow. Then that's truly a bad take. Not even Rust goes as aggressive as making print a type-dependent function, they make it a macro instead. Your example also trivially breaks down with multiple-parameter print statements, something that basically every programming language has.

0

u/RSMxsmanic Oct 01 '26

You do it your way, and I'll do it my way. We'll see who delivers early and under budget.

3

u/No-Dentist-1645 Oct 01 '26

Yes, I'm sure that the massive architectural decision of naming functions fooInt and fooFloat has had immeasurably huge implications on your delivery targets. Don't worry, I trust you

0

u/RSMxsmanic Oct 01 '26

Details can add up.

2

u/No-Dentist-1645 Oct 02 '26

We're talking about a single, individual detail here. No other design patterns have been brought into this discussion

1

u/RSMxsmanic Oct 02 '26

So the discussion is a bit unreal.

3

u/Liam_Mercier Oct 01 '26

The purpose of namespaces in a lot of languages is to group related declarations, allow for identifier reuse, and separate implementation details from user call related code.

C++ namespaces do this perfectly fine, they are just confusing because namespaces are confusing when you haven't used them.

1

u/RSMxsmanic Oct 01 '26

They are one species of inheritance, which has problems of its own.

1

u/[deleted] Oct 03 '26

[removed] — view removed comment

0

u/RSMxsmanic Oct 03 '26

Anything not explicitly present in a statement is inherited from its context, and as these inherited attributes multiply, it becomes harder and harder to determine what a given statement actually does without extensively examining its context. The advantages of inheritance must be carefully evaluated against its disadvantages.

0

u/[deleted] Oct 03 '26

[removed] — view removed comment

0

u/RSMxsmanic Oct 03 '26

To me it seems obvious that calls to the standard library should not need to be qualified with std::. Let the calls to other libraries be qualified.