简体   繁体   English

可以使用C ++ 14标准库的哪些部分以及哪些部分将成为constexpr?

[英]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_liststd::pairstd::tuplestd::complexstd::bitsetstd::array看起来像是被标记为constexpr批发的主要候选者。

Questions : 问题

  • which parts of the Standard Library will now be marked constexpr ? 其标准库的部分现在被标记constexpr
  • which other parts could be marked constexpr ? 哪些其他部分可以标记为constexpr
  • eg why aren't the functions from <cmath> and <algorithm> marked constexpr ? 例如, <cmath><algorithm>的函数为什么不是constexpr
  • are there backwards compatibility reasons not to do so? 是否有向后兼容的原因不这样做?

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
  • All of std::pair 's operator comparisons 所有std::pair的运算符比较
  • std::get for std::pair and std::tuple . std::get for std::pairstd::tuple
  • std::make_tuple
  • All of std::tuple 's operator comparisons 所有std::tuple的运算符比较
  • All of std::optional 's operator comparisons 所有std::optional的运算符比较
  • All of std::optional 's constructors (save for move) 所有std::optional的构造函数(除了移动)
  • operator[] and size for std::bitset and other containers. operator[]std::bitset和其他容器的size
  • All of 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添加列表,您可以检查: N3469N3470N3471

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::tuplestd::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添加到标准库中的更多例程中。

  • forward_as_tuple forward_as_tuple
  • the operator () method of all the comparison / logical / bitwise named operators. 所有比较/逻辑/按位命名运算符的operator ()方法。 ( less , greater , plus , minus , bitwise_and , logical_or , not1 - and the rest) lessgreaterplusminusbitwise_andlogical_ornot1 - 以及其余的)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM