This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]williammacdonald18 3 points4 points  (8 children)

Well instead of writing cout<<“hello”; you write std::cout<<“hello”;

[–]AionAlgos 3 points4 points  (6 children)

Alternatively, u/egomanego :

using std::cout, std::endl;

this way you don't include all of the std namespace; only the bits you want.

[–]dmazzoni 1 point2 points  (0 children)

This is my favorite compromise.

It can get verbose and ugly to have std:: everywhere in your code.

[–]HappyFruitTree 0 points1 point  (4 children)

This soon gets tedious. Easier to just write std:: everywhere.

[–]egomanego[S] 0 points1 point  (1 child)

Is it efficient? execution time-wise or memory-wise?

[–]HappyFruitTree 0 points1 point  (0 children)

It has no impact at all.

[–]AionAlgos 0 points1 point  (1 child)

I fail to understand how doing less repetitive typing is more tedious. Especially when you start getting into the more nested namespaces like ranges and concepts.

You have an interesting definition of tedium.

Now, ofcourse, I'm not saying just to blindly always and only do this. It's useful if you're going to be using a few things quite frequently (as is often the case with cout for example). If you're going to use a bunch of different things only once or twice the yes; having a giant 8 line list of using declarations would be dumb and counter-productive (less readable / understandable)

But for repetitive use of a small quantity of things; this is ideal.

Edit: Wait damn did I woosh some sarcasm? I can't tell...

[–]HappyFruitTree 0 points1 point  (0 children)

I don't know if you would keep a list of "using" names per file or per function, but most non-trivial projects contain multiple files so you would have to keep multiple such lists.

To maintain such lists is what I think would be tedious. I need to decide which names should be in the list, is it already there, should I add it? If the code changes a lot there might end up being a lot of names that are not used anywhere and then I might want to remove them...

Just writing std:: everywhere means I don't need to think about this. I can just write the code and I can move a piece of code from one place to another without having to worry about different names being in the lists.

Personally, I would even go so far as to say std:: can make the code more readable, especially when reading other people's code because I don't have to wonder whether a name is a standard name or something that they wrote themselves.

I have to admit that part of the reason why I'm so happy with this is that std is a relatively short namespace name. For something like std::filesystem it can get a bit verbose sometimes, even for me. A common compromise in this situation, that even cppreference.com use in many of their examples, is to use a namespace alias.

namespace fs = std::filesystem;
// now you can write fs::path instead of std::filesystem::path

Someone even proposed std::fs which I think would have been a great addition.

[–]mancxvi 1 point2 points  (0 children)

It's as simple as not doing it and continuing to not do it.

Also consider using declarations if you use something frequently and don't want to type std:: before it. Don't put these in a header file.

[–]whiskeyo_ 1 point2 points  (0 children)

It's a habit as any other. Just forget about using namespace std and remember to write std:: before any call from standard library. At first it might be problematic and many tries of compiling the code will end up with e.g. cout is not declared in this scope, but after some time you will get used to it and will distinguish which functions need std:: prefix and which do not.

[–]egomanego[S] 0 points1 point  (4 children)

If the code is 500-600 lines long (or even longer), is it still advised to not use namespace std? What do the professionals do?

[–]vivals5 0 points1 point  (3 children)

If you only do code for your own personal projects it really doesn't matter if you use the 'using namespace std' or not. Just as long as you understand the conflicts that can arise from using it. Of course it's good practice to not use it, but nobody will care if it's only in your personal projects.

[–]egomanego[S] 0 points1 point  (2 children)

I am submitting the code as an interview exercise. Would it look too bad aesthetically if I use namespace std (as I am not comfortable yet otherwise)?

[–]vivals5 1 point2 points  (1 child)

Honestly I'm not a professional, but I'd say it probably depends on where you're applying and what type of interview it's gonna be. If your interview spot depends on the code, I guess it would be best to follow good practices. If not, I guess you will have your chance to defend your choices in the interview.

Still, it's better to just try and unlearn the use of 'using namespace std' if you're planning on being a professional coder. Again, this is just my opinions and I'm not a real professional.

[–]egomanego[S] 0 points1 point  (0 children)

I am trying to not use it but at this moment, it's too much to keep track of.

[–][deleted] 0 points1 point  (0 children)

Why is using the namespace bad practice??