为什么 std::array 的 operator==() 没有被标记为 constexpr?
Why isn't std::array's operator==() marked constexpr?
very natural to want to compare std::array
's at compile time; and its operator==()
is obviously constexpr
'able. Yet - it isn't 标记为 constexpr
。这是故意的还是疏忽?并且 - 它被保留下来的原因是什么(显然也在 C++17 中)?
基本原理可能是这样的:如果包含的类型的 ==
也是 constexpr
,则数组的 ==
只能是 constexpr
。
由于容器不能强制执行,它不能(通常)提供 operator==() constexpr
。
P0031 解释了为什么它没有提出 constexpr
比较:
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.
例如,==
可以根据 std::equal
来实现,在适当的情况下,它可以调用高度优化但决定不是 constexpr
memcmp
。为 ==
创建 constexpr
将在没有特殊编译器帮助的情况下排除此优化。
very natural to want to compare std::array
's at compile time; and its operator==()
is obviously constexpr
'able. Yet - it isn't 标记为 constexpr
。这是故意的还是疏忽?并且 - 它被保留下来的原因是什么(显然也在 C++17 中)?
基本原理可能是这样的:如果包含的类型的 ==
也是 constexpr
,则数组的 ==
只能是 constexpr
。
由于容器不能强制执行,它不能(通常)提供 operator==() constexpr
。
P0031 解释了为什么它没有提出 constexpr
比较:
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.
例如,==
可以根据 std::equal
来实现,在适当的情况下,它可以调用高度优化但决定不是 constexpr
memcmp
。为 ==
创建 constexpr
将在没有特殊编译器帮助的情况下排除此优化。