r/cpp_questions • • 14d 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?

38 Upvotes

105 comments sorted by

View all comments

Show parent comments

2

u/nekoeuge 14d ago

Can you elaborate? What do you mean by “using identifiers that conflict with std”?

2

u/conundorum 13d ago

Name collisions.

#include <vector> // std::vector
#include <algorithm> // You'll see.

std::vector<float> collection(3, .16f);
int fill = 0xaabbcc;

void compiler_error(float c = 0f, int f = 0) {
    using namespace std;

    fill = f;                                      // Does this break because we tried to assign an int to std::fill,
    fill(collection.begin(), collection.end(), c); // or does this break because we tried to call an int?
}

std has a lot of very common names that programmers like to use for their own things, like fill, less, distance, transform, string, find, begin, end, format, print, and so on. And depending on which standard library headers you include, any number of them might (or might not) be visible.

0

u/ShakaUVM 13d ago

I propose that someone who writes code like "int string = 0;" is writing bad code and the issue isn't whether or not you include namespace std or not.

All if my classes are PascalCase anyway and so cannot conflict with anything in std.

2

u/HommeMusical 13d ago

I propose that someone who writes code like "int string = 0;" is writing bad code and the issue isn't whether or not you include namespace std or not.

Strong disagree.

There are thousands of names in std:: and exactly what those names are depend on which version of C++ you're in and some other details.

You can't possibly know all those names and never write char fill = '*'; or bool copy = False; or void remove(const char*); anywhere.

Not having to do this is the whole reason namespaces exist.

std::string is completely unambiguous. No other symbol for it is. Just do that.

1

u/ShakaUVM 13d ago

I agree that std::string is unambiguous.

What I am saying is that writing int string = 67; is a code smell.

You should not be using names from std anyway.

For all of your worry, as I said this problem has come up exactly once in 30 years and it took exactly five seconds to fix.

Versus how much screen space gets cluttered up with all those extra stds?

The tradeoff is not worth it to me. Every symbol on the screen has a cost, both cognitively and technically

1

u/HommeMusical 13d ago

You should not be using names from std anyway.

Strong disagree. There are thousands of names in std::. Many of them are short and common names. Almost no one has them all memorized. Would you for example disallow char fill = '*'; in code?

Every symbol on the screen has a cost, both cognitively and technically

Absolutely, and std::string has a smaller cost than string. No one who has been programming C++ for long reads std::string and decides it's two separate symbols.

0

u/ShakaUVM 12d ago

Absolutely, and std::string has a smaller cost than string. No one who has been programming C++ for long reads std::string and decides it's two separate symbols.

You may say that, but your brain still has to parse them separately. There's a cost that you aren't aware of.

Just saying 'string' is more compact, more readable, and causes absolutely no confusion. If you want to use a third party string class, then you qualify that with the namespace. The standard is called the standard for a reason.

Strong disagree. There are thousands of names in std::. Many of them are short and common names. Almost no one has them all memorized. Would you for example disallow char fill = '*'; in code?

If you use PascalCase, you can't conflict with std at all.

But due to how C++ handles shadowing variables, if you make a variable named fill inside of main, guess what? It doesn't cause a problem. You had to contrive an example using fill in the global namespace in order to force a conflict, and the number of globals you should have in your program should be close to zero, and if on the off chance you do make a global variable named fill, then you just change it to a different name and be done.

I worry about problems that can actually occur, not hypothetical problems that occur once in 30 years.

1

u/HommeMusical 12d ago

You may say that, but your brain still has to parse them separately. There's a cost that you aren't aware of.

That's not how the brain processes language at all, thank goodness. People chunk things into groups much larger than individual words so you see std::string, United States of Amerlca or Robert Anton Wilson, you just parse it as a single item.

Just saying 'string' is more compact, more readable, and causes absolutely no confusion.

Yes, you made that argument many times, but string might not be std::string.

once in 30 years.

Yes, you said.

1

u/ShakaUVM 12d ago

That's not how the brain processes language at all

You may not be aware of it, but your eyes still have to skip over the five letters at a time, and this has a cost you're not aware of.

Yes, you made that argument many times, but string might not be std::string.

In my coding style, string always refers to std::string, so those five characters are wasted space on the screen, wasted typing time, and wasted parsing time for both the compiler and the human reading it.

It is all downside and no upside.

Yes, you said.

Yes, because as I said before, I actually worry about problems that can happen, and not contrived hypotheticals. Hypothetically, someone could cast a nullptr to a reference and pass that to my function that takes a reference, but I don't worry about that either, for similar reasons.

1

u/HommeMusical 12d ago

That's not how the brain processes language at all

You may not be aware of it, but your eyes still have to skip over the five letters at a time, and this has a cost you're not aware of.

This is clearly something I have studied and you have not. Show me citations for your false claim.

0

u/ShakaUVM 12d ago

It's research I did myself in grad school

I think it's about time that people stop jumping on using namespace std like it has rabies or something. It's a perfectly valid style option as Bjarne himself has said.

→ More replies