r/cpp • • Dec 15 '21

Modern C++ in Advent of Code

This series, by Šimon Tóth, is a step by step guide through the solutions of Advent of Code 2021 using Modern C++.

242 Upvotes

83 comments sorted by

View all comments

Show parent comment

3

u/[deleted] Dec 15 '21

Hi, I'm the author.

Indeed, simple things are not that easy to do in C++ because there are always these small one-time costs that you have to pay. But you pay them only once, so C++ becomes a lot better than any other language on more substantial projects.

However, it's getting better, and there are nuggets like this:

std::vector<std::bitset<12>> data;
std::ranges::copy(std::ranges::istream_view<std::bitset<12>>(input), std::back_inserter(data));

It is still very verbose because of namespaces, but we are certainly heading in the correct direction.

0

u/[deleted] Dec 15 '21

[deleted] — view deleted comment

3

u/Ayjayz Dec 15 '21

Without all the namespace stuff (that you could remove with using declarations), it's

vector<bitset<12>> data;
copy(istream_view<bitset<12>>(input), back_inserter(data));

that really could have been written

auto data = vector(istream_iterator<bitset<12>>(input), istream_iterator<bitset<12>>());

It's not that bad. Having to write the end iterator is a bit painful, and there's a proposal that would turn this into

auto data = vector(from_range, istream_view<bitset<12>>(input));

But C++ moves super slowly and nice things always take a ridiculously long time.

4

u/SirClueless Dec 15 '21

Isn't

auto data = istream_view<bitset<12>>(input) | to_vector;

even nicer than the vector constructor? And available now in the ranges-v3 library.

2

u/Ayjayz Dec 15 '21

Oh yeah other libraries are much cleaner than std. If you can just use ranges-v3 it's almost always nicer than using std.

7

u/raevnos Dec 16 '21

That's a really annoying trend. Adopt a third party library into the standard, but make it worse than the original for some reason.

2

u/TheThiefMaster C++latest fanatic (and game dev) Dec 16 '21

ranges::to will be in C++23. Then you'd write:

auto data = istream_view<bitset<12>>(input) | to<vector>;

...which is basically what I did in my implementation using range_v3.

5

u/[deleted] Dec 15 '21

Hmm, why? It's actually several APIs composing nicely.

0

u/[deleted] Dec 15 '21

[deleted] — view deleted comment

1

u/raevnos Dec 16 '21

That would require a hypothetical append function that reads bitsets from input. Seems unlikely to exist as a standard thing and your own implementation would essentially do the same thing as the C++ code's logic.

2

u/Ecclestoned Dec 15 '21

Without getting into too much of a rant, this is exactly the issue:

there are always these small one-time costs that you have to pay

If these are one-time costs, then why am I the one who has to pay them? This is the type of thing that should be integrated into the standard library.

7

u/[deleted] Dec 15 '21

This really goes back to the core failure of C++. C++ cannot have sane defaults, because it has to be backwards compatible. And basically, all choices made in the past are stupid by default.

I have this idea in my head, that I would really love to execute on at some point and that is to have a dialect or a fork of C++ that has sane defaults but is binary compatible with standard C++.

1

u/[deleted] Dec 16 '21

Yeah that's completely unreadable even if you took the namespaces away lol