It's very natural to want to compare std::array
's at compile time; and its operator==()
is obviously constexpr
'able. Yet - it isn't marked constexpr
. Is this intentional or an oversight? And - what's the reason it was left that way (apparently in C++17 as well)?
P0031 explained why it didn't propose constexpr
comparisons:
Currently comparisons and
swap
/fill
may be implemented with the help of algorithms from<algorithm>
header. Marking comparisons with constexpr will break that ability and will potentially lead to performance degradations.
For example, ==
can be implemented in terms of std::equal
, which - in appropriate cases - can call the highly-optimized-but-decidedly-not-constexpr
memcmp
. Making constexpr
for ==
will rule out this optimization without special compiler assistance.
The rationale might be this: ==
of an array can only be a constexpr
if the contained type's ==
is a constexpr
, too.
Since the container can't enforce that, it can't (generally) offer a operator==() constexpr
.
constexpr
... so that is not it. (PS - Not my downvote.) –
Lime constexpr
for a templated function does not mean that the function is always constexpr
, it just say this function may be constexpr
for some types. –
Magnific © 2022 - 2024 — McMap. All rights reserved.