Search code examples
c++c++11comparisonconstexprstdarray

Why isn't std::array's operator==() marked constexpr?


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)?


Solution

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