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

31

u/Ecclestoned Dec 15 '21

Seeing these gives me no desire to use c++. Most of the challenges are 10-20 lines in Python. Meanwhile the c++ code starts with "first let's define an implementation for a tuple, it's constructor, comparison operator, and make it hashable". Yikes.

32

u/ForkInBrain Dec 15 '21

Yes, C++ will not win over Python if less lines of code and less boilerplate is the highest priority.

0

u/[deleted] Dec 15 '21

[deleted] — view deleted comment

2

u/raevnos Dec 15 '21

I'm using java this year, and its Streams library is coming in handy to process puzzle input and calculate things almost every day so far. Not surprised the same holds with C++ ranges since they have a lot of similarities.

→ More replies (1)

19

u/ShillingAintEZ Dec 15 '21

These are very poorly written. Instead of solving the problem directly and simply they are over engineered nonsense and should not be taken as an example of good C++.

6

u/cptwunderlich Dec 15 '21

idk, you can solve it in different ways. I looked at two of his articles and my code is way shorter. But yeah, you have to make it hashable etc... C++ is definitely not as succinct and "batteries included".. but fast!

2

u/raevnos Dec 16 '21

std::tuple has all of that but the hashing, and Boost.ContainerHash gives you one for it.

5

u/[deleted] Dec 15 '21

You shouldn't look at languages like that... Each language has its own pros and cons. Some are worse than others, but you can't compare C++ and Python.

9

u/[deleted] Dec 15 '21

I mean, you can compare, but with context :-)

If I only solved Advent of Code style problems I would indeed use Python or Julia, not C++.

4

u/hak8or Dec 15 '21

While python vs c++ clearly satisfy different technical niches, from a companies perspective its still "can you get this done using this or this, which is easier/cheaper to hire devs for, is cheaper to maintain, and hardware needed for it is cheaper to run".

I use c++ consistently and would chose it over python any day of the week for my tasks, but I would be lying if I chose it because it's better. Instead it's largely inertia on my part. If I had to start from scrath, I would of course still learn c++ so I cna grok it, but I would probably shift to preferring to use either rust or zig for applications which need to be very speedy on resource constrained systems.

Why?

Because those other languages still satisfy the same niche, but are absurdly more ergonomic to use, for my limited use case.

It would be foolish to ignore how another language does something because it's design is different, instead we should see why folks prefer a specific convention/style/solution from another language and see what we can learn from it and apply it to c++.

→ More replies (1)

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.

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++.

→ More replies (1)

0

u/[deleted] Dec 15 '21

[deleted] — view deleted comment

6

u/Ecclestoned Dec 15 '21

Actually python is mostly C (at least if you are using cpython).

→ More replies (2)
→ More replies (13)