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.

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.

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