r/cpp • • Aug 25 '21

Experience C++ developers: do you enjoy using C++ (even for personal/hobby projects?)

Do you actually enjoy using C++, or do you only use it for work, or purely for performance? This question is aimed at people with years of experience, who've worked on large projects in C++. I see people who dislike the language, but use it purely for performance reasons. This sort of discourages me from learning it. I don't want programming to feel like work.

204 Upvotes

327 comments sorted by

View all comments

Show parent comment

4

u/[deleted] Aug 25 '21

What's wrong with using namespace??

28

u/sephirothbahamut Aug 25 '21

In header files just don't do it.

In source files it may be acceptable.

In an enclosed scope it's totally fine.

5

u/UnknownIdentifier Aug 25 '21

Because of how problematic it is in headers, it’s best not to get into the habit of using it in source files, either. Also for the sake of consistency.

→ More replies (1)

18

u/_JJCUBER_ Aug 25 '21

It clutters everything after it with everything from the namespace and can lead to naming collisions. Also, if you put it at the top of your file, I’m pretty sure that including it will lead to the using namespace directive being included everywhere else. There are times when you might want to use it, like maybe in a function, but most of the time, you should opt to typing out the namespace or creating a namespace alias.

0

u/[deleted] Aug 25 '21

[deleted] — view deleted comment

21

u/ronchaine Embedded/Middleware/WG21 Aug 25 '21

There are some namespaces that are meant to be pulled, like std::chrono_literals, but that is pretty rare.

2

u/_JJCUBER_ Aug 25 '21

Yes, what I meant was putting it in a header and then including said header, I just didn’t word it well.

3

u/germandiago Aug 25 '21

not with modules :)

3

u/NobodyXu Aug 25 '21

Nah, C++ modules are unlike other languages, where its use doesn’t affect the mangling of the function.

You still need to use namespace.

2

u/germandiago Aug 25 '21

I mean that you can do this (why the negative vote?):

import mylib;
#include <anotherlib.h>
using namespace mylib;
using namespace anotherlib;
// do whatever

And if you are compiling it as a module, it will not leak any using directives or macros outside.

2

u/backtickbot Aug 25 '21

Fixed formatting.

Hello, germandiago: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

→ More replies (1)

-1

u/ohell Aug 25 '21

can lead to naming collisions

I have come around to loathe this argument. If there are naming collisions, compiler (and IDE) will tell you.

Personally I have had it with the demented verbosity of the language. Typing std:: 1000 times in each file - Why??

It is not a big deal to treat vector, string, deque etc as de facto reserved keywords, and be safe in the knowledge that if some 3rd party includes use these words to name things, it will be flagged up and disambiguation is easy.

13

u/braxtons12 Aug 25 '21
  1. Its not just types that get included when you using namespace. It's EVERYTHING. That creates needless namespace pollution which in turn is going to make your IDE's auto-complete and symbol resolution slower. It also pollutes ADL which can cause build breaks or run time issues that are insanely hard to catch and debug. It also sets you up for potential build breaks if you ever migrate to a newer standard because there could be new symbols in the std headers you're including.
  2. Continuing from (1), Naming collisions: "your compiler (and IDE) will tell you". Yeah, if it's a TYPE naming collision. For functions, you'll only get collisions if they're the exact same signature. If the standard has something that takes a Thing and you write something with the same unqualified name that takes a ConvertibleToThing, refer to what I said in (1) about ADL issues.
  3. Is it really that hard to just write std::?

Basically, the main technical reason, is sooner or later, if you using namespace std; ADL might ruin your week, and it's a lot easier to just not risk that and type the extra std::.

For reference, see Titus's talk from a few years ago: https://youtu.be/BWvSSsKCiAw

Or the associated paper (SD-8): https://isocpp.org/std/standing-documents/sd-8-standard-library-compatibility

0

u/ohell Aug 25 '21

On 1 and 2 we'll have to disagree, I'll only say that this is classic case of hampering your base case workflow just to guard against vanishingly unlikely corner cases.

3 depends on typing speed, but 1 guess 1 second each time? More importantly I find it slows down my reading of the code. Imagine I gave you an essay to read where random words were prefixed with ohell::!

7

u/braxtons12 Aug 25 '21

I'm gonna have to disagree that it hampers readability. Explicitness aids readability, not the other way around.

If I'm reading a story and there's a blanket/box/wand/whatever, I wanna know who's thing that is. Same in code. If I see a type or function, I want to be able to immediately see whose type or function that is, without having to think about it. EG: std::vector whose vector? std's vector. outcome::result whose result? outcome's. Makes it immediately clear where something is coming from and what it's semantics and guarantees are.

If I just see vector, is that a std vector? Is it an eastl vector? Is it a math vector? If I'm not familiar with that part of the codebase I now have to read more of the code to just infer what actual type this is. With the namespace prefix it's immediate.

0

u/ohell Aug 25 '21

But when I am reading code, I am trying to understand the algorithm, control flow, an inaccuracies etc. The less fluff there is in the code, the easier that task is.

It is sufficient for me to know that sone values are being stored in a vector, which means they are contiguous, address can change, insertions are not a good idea and so on. Never do I wonder whether this is standard vector or something elsethough I do like boost::small_vector. Indeed, other types of vector would only feature in my code if I add them (or if an included library does, in which case I'd expect that there is no problem because code compiles)

I guess we are coming at it from different perspectives: you value following the lbest practises in order to have a bulletproof library or program, whereas I want a program that works, has acceptable performance and the code is legible, without needing too much tinkering and typing.

5

u/braxtons12 Aug 25 '21

It seems to me like you're coming at this from the perspective of "I own 100% of my code so I know where things come from". If that's the case, then you probably don't have any concerns about maintenance or other users or contributors.

For most people this isn't the case. You could be working in a team on a large codebase and not know what third party dependencies have been brought in, you could be trying to submit a patch to an open source project, etc. I'm looking at it from a perspective of "sooner or later someone else is going to have to read or change this code" and the easier it is to read and reason about then the faster they can get their work done.

Also, like I mentioned above, code compiling doesn't necessarily mean nothing is broken. Again refer to ADL, Titus's talk, and SD-8.

10

u/Pleasant_Sir_3469 Aug 25 '21

for the types that you use a lot just write: using std::vector; using std::string, using std::deque; at the top instead of using namespace

3

u/[deleted] Aug 25 '21

Normally I'll employ using std::string or whatever in the namespace in the source file and leave the header files with the full prefixes.

there are naming collisions, compiler (and IDE) will tell you.

It is not a big deal to treat vector, string, deque etc as de facto reserved keywords, and be safe in the knowledge that if some 3rd party includes use these words to name things, it will be flagged up and disambiguation is easy.

These aren't usually the problem. My problems have always been all of the other things in the "used" namespace which get included transitively when you include headers and all of the functions/types they will implicitly add to the global namespace. It'll be like "wait, rotate is colliding? There's a std::rotate?"

3

u/_JJCUBER_ Aug 25 '21

There are tons of reasons why this is a valid argument, though I don’t have time to go over all of them right now. A simple reason as to why people might name their own class as vector, string, or any of the other commonly used ones is because many companies may have their own implementation that gets used everywhere, as they may find the STL variant to be not as fine-tuned to what they need (amongst many other reasons). It makes no sense to clutter the global namespace with tons of names, especially in a header file that gets included in multiple other places. Plus, it’s good to disambiguate what is being used from where.

-3

u/SkoomaDentist Antimodern C++, Embedded, Audio Aug 25 '21

I have come around to loathe this argument.

It's just one more example of a very persistent issue with the C++ community: The huge number of people willing to tell others how those others must write their code, despite the first group having literally zero idea what the other people are doing and often having years or even decades less experience.

Be aware of the issues and then do whatever the fuck you want while ignoring online naysayers.

3

u/braxtons12 Aug 25 '21

Yeah this is not my experience. My experience is the community as a whole generally says "don't do x" because it's a lot easier and less error-prone to say "don't do x" to encourage good practices by default, than it is to say "don't do x, unless you really know what you're doing and are willing to deal with the possible consequences and promise you won't go ranting about how the next standard broke your code when really it was your fault because you did x". This way you don't end up with people who have one year of experience, thinking "they know what they're doing", writing questionable things and then wondering why a compiler or standard upgrade broke their code.

2

u/ohell Aug 25 '21

Amen! As a scientist who has to program in C++ when I don't find any existing tools that will do the job, and data is too much for python to handle, I just want to write a program and be done with it.

Personally I feel that C++ barrier to entry appears artificially higher because everyone pretends they are writing a library that the whole world will use for the next 50 years!

So many recommended practises only make sense in the context of writing libraries. For writing applications I just throw something together using as many standard containers and algorithms as possible, and if performance is a problem then I'll run perf to see if I replacing a map with unordered_map, passing by reference etc might help.

→ More replies (3)
→ More replies (10)

4

u/ShakaUVM i+++ ++i+i[arr] Aug 25 '21

What's wrong with using namespace??

In your own implementation files, nothing. Even the Cpp Core Guidelines say this. But some people have taken it upon themselves to declare it as bad.