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 comments

1

u/Adverpol Aug 25 '21

fwiw I agree with you, I don't understand why people don't mind having std::vector<std::pair<std::string, std::string>> instead of vector<pair<string, string>>. I do think that gets in the way, and no I don't think adding using to the top of my cpp file helps because that means that all of my header files, i.e. the public API, still has this clutter.

3

u/braxtons12 Aug 25 '21

The short version of the reason is we like things to not break our, or our users', code.

Doing a using namespace std; is asking for ADL issues to happen eventually.

If you don't want to deal with verbose type names, it's fine to do a using for a type, eg using std::vector; or similar, in a source file, but don't pollute your headers with it and force users to deal with that.

I would also argue that explicitness is not clutter, it makes readability and maintainability better because you immediately see where things come from.

For reference on why it's bad, along with a lot of other things you should not do, 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

1

u/Adverpol Aug 26 '21

I would also argue that explicitness is not clutter, it makes readability and maintainability better because you immediately see where things come from.

I keep reading this with respect to std::, and I couldn't disagree more. I don't think it adds anything but unnecessary clutter.

I would just like it that there are a couple of standard types that are "just there", like vector, string, tuple, pair, ... You know, the way rust does it... All of the reasons not to do it are the reasons why I do use std:: when writing code, but that doesn't mean I have to like it.

1

u/braxtons12 Aug 26 '21

Except in Rust, "tuple", and "pair" aren't library items, they're language constructs, and Rust doesn't suffer from ADL, overloading, or ODR, so the ONLY problem that can happen is a build break.

1

u/Adverpol Aug 27 '21

I'm not sure what the point is you're trying to get across? Mine is that I think having to use std:: everywhere hurts readability. I understand that there are reasons that we are stuck with this, that doesn't change the fact that I think it's a shortcoming of C++. I do type std:: for all my types, I just don't like it, and I disagree with people arguing that it's actually a good thing.

1

u/braxtons12 Aug 27 '21

You used Rust as example of how you would like C++ to behave when it comes to standard library features. My point was Rust only gets away with how it does things because it doesn't have to deal with any of the complicated features we have in c++ that make c++ what it is, so using Rust as an example of how things could or should be in this regard is rather silly.

Also, IMO shortcoming implies something that could be changed or fixed in a relatively compatible way but just either hasn't been or isn't going to be. But to change this, it would require changes so drastic it would literally be a different language.

1

u/ohell Aug 25 '21

And then,

std::for_each(std::begin(vector_of_string_pairs), std::end(vector_of_string_pairs), [] (auto &&sp){ std::cout << std::get<0>(sp) << ' ' << std::get<1>(sp); });

std::fml ¯\(ツ)/¯

1

u/braxtons12 Aug 26 '21

Why would you not just:

std::for_each(vec.begin(), vec.end(), [](auto&& p) {
    std::cout << std::get<0>(p) <<  ' ' << std::get<1>(p);
});

Or even cleaner:

for(auto&&[first, second] : vec) {
    std::cout << first << ' ' << second;
}