[英]Which parts of the C++14 Standard Library could be and which parts will be made constexpr?
With the new relaxed C++14 constexpr rules , compile-time programming becomes a lot more expressive. 使用新的轻松的C ++ 14 constexpr规则 ,编译时编程变得更具表现力。 I wonder whether the Standard Library will also be upgraded to take advantage.
我想知道标准图书馆是否也会升级以便利用。 In particular,
std::initializer_list
, std::pair
, std::tuple
, std::complex
, std::bitset
and std::array
seem like prime candidates to be marked constexpr
wholesale. 特别是,
std::initializer_list
, std::pair
, std::tuple
, std::complex
, std::bitset
和std::array
看起来像是被标记为constexpr
批发的主要候选者。
Questions : 问题 :
constexpr
? constexpr
? constexpr
? constexpr
? <cmath>
and <algorithm>
marked constexpr
? <cmath>
和<algorithm>
的函数为什么不是constexpr
? which parts of the Standard Library will now be marked constexpr?
标准库的哪些部分现在将标记为constexpr?
From the draft that I've looked at for C++14, N3690 , the following will be changed to constexpr
thus far (In comparison with the C++11 standard)†: 从我为C ++ 14, N3690查看的草案中, 到目前为止 ,以下内容将更改为
constexpr
(与C ++ 11标准相比)†:
std::error_category
's default constructor std::error_category
的默认构造函数 std::forward
std::move
std::move_if_noexcept
std::pair
's operator comparisons std::pair
的运算符比较 std::get
for std::pair
and std::tuple
. std::get
for std::pair
和std::tuple
。 std::make_tuple
std::tuple
's operator comparisons std::tuple
的运算符比较 std::optional
's operator comparisons std::optional
的运算符比较 std::optional
's constructors (save for move) std::optional
的构造函数(除了移动) operator[]
and size
for std::bitset
and other containers. operator[]
和std::bitset
和其他容器的size
。 std::complex
's operator comparisons std::complex
的运算符比较 † Since I did this manually, you can expect some errors :( † 由于我手动执行此操作,您可能会遇到一些错误:(
For another possibly more correct list of constexpr
additions you can check: N3469 , N3470 , and N3471 对于另一个可能更正确的
constexpr
添加列表,您可以检查: N3469 , N3470和N3471
which other parts could be marked constexpr?
哪些其他部分可以标记为constexpr?
Most of the stuff that could be constexpr
( std::numeric_limits
evaluation, std::tuple
and std::pair
constructors, etc) were already marked as constexpr
in the C++11 standard. 大多数可能是
constexpr
的东西( std::numeric_limits
评估, std::tuple
和std::pair
构造函数等)已经在C ++ 11标准中标记为constexpr
。 There was a bug in which std::ratio
's time points and other components weren't marked as constexpr
but it was fixed in N3469. 有一个错误,其中
std::ratio
的时间点和其他组件未标记为constexpr
但它已在N3469中修复。
Something that would benefit from constexpr
additions would be std::initializer_list
, which didn't get any this time around (and I'm unsure if there have been any proposals to allow it). 可以从
constexpr
添加中获益的东西是std::initializer_list
,这次没有得到任何东西(我不确定是否有任何提议允许它)。
are there backwards compatibility reasons not to do so?
是否有向后兼容的原因不这样做?
Since this is an extension , most stuff won't be broken since older code will still compile as-is and nothing is now ill-formed. 由于这是一个扩展 ,大多数东西都不会被破坏,因为旧的代码仍然会按原样编译,现在什么都没有形成。 However adding
constexpr
to older things that didn't have it could lead to some surprising results if you didn't expect it, such as the example provided here (Thanks TemplateRex) 然而,将
constexpr
添加到没有它的旧东西可能会导致一些令人惊讶的结果,如果你没想到它,例如这里提供的例子(谢谢TemplateRex)
Last week (Sep 23-28, 2013) the standards committee added constexpr
to more routines in the standard library. 上周(2013年9月23日至28日),标准委员会将
constexpr
添加到标准库中的更多例程中。
operator ()
method of all the comparison / logical / bitwise named operators. operator ()
方法。 ( less
, greater
, plus
, minus
, bitwise_and
, logical_or
, not1
- and the rest) less
, greater
, plus
, minus
, bitwise_and
, logical_or
, not1
- 以及其余的) @TemplateRex: We're getting closer to sorting arrays at compile time. @TemplateRex:我们在编译时越来越接近排序数组了。
However, we also resolved LWG issue 2013, stating that standard library implementers do NOT have the freedom to make calls that are not defined in the standard as constexpr
as constexpr
, since that kind of difference between implementations could change the behavior of some code. 但是,我们还解决了2013年的LWG问题,指出标准库实现者不能自由地将未在标准中定义的调用作为
constexpr
进行constexpr
,因为实现之间的这种差异可能会改变某些代码的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.