From: Peter Dimov (pdimov_at_[hidden])
Date: 2023-12-01 15:18:25


Alexander Grund wrote:
> Am 30.11.23 um 18:49 schrieb Andrey Semashev via Boost:
> > I don't want to include <functional> just for std::reference_wrapper.
> > I don't want to include <algorithm> just for std::swap.
> > I don't want to include <utility> just for std::move.
> > I don't want to include <functional> just for that trivial function
> > object wrapper.
> I collected all such statements from your replies and would like to
> object: Reimplementing std-library components to avoid an include looks
> very wrong to me.
>
> Not only is it likely premature optimization: User code including your
> headers likely included those files already making the include "free".
> Especially as many std-headers transitively include <utility> or even
> <algorithm>.
> And 2nd: With the collection above there are now 2 points obvious that
> <functional> is not included "just for x" and then "just for y". So now
> you have 2 reasons already to include it.
> And finally this is about readability: Everyone knows what
> `std::move(that)` does, but it is not that obvious for `static_cast<
> unique_resource_data&& >(that)` Similar for swapping "m_allocated" being
> expanded to 3 lines instead of `std::swap` obfuscating what it does. And
> ref_wrapper begs the question if and how it is different to
> std::reference_wrapper
>
> TLDR: I'd strongly suggest to use the std-library where possible to a)
> not reinvent the wheel and b) improve readability by using existing
> idioms/names.

Unfortunately, including <functional> carries a very real cost nowadays,
and this does lead to user complaints.

<functional>, -std=c++03: 721 lines
<functional>, -std=c++11: 12491 lines
<functional>, -std=c++14: 13661 lines
<functional>, -std=c++17: 33458 lines
<functional>, -std=c++20: 37201 lines
<functional>, -std=c++23: 37252 lines

(GCC 11.3.0)

The story of <algorithm> is similar:

C++03: 9279
C++11: 14332
C++14: 14854
C++17: 34143
C++20: 56796
C++23: 56904

<utility> is cheaper:

C++03: 218
C++11: 3271
C++14: 3504
C++17: 4054
C++20: 6411
C++23: 6462

Few know this, specifically how much things changed with C++17/C++20.

Incidentally, std::swap is in <utility> starting from C++11.

Having to include 37K lines just to get std::less or std::hash is a crime
against humanity, but at least modules will fix everything no more than
15 years from now. Maybe even less!