C ++ 14标准库的哪些部分可以和哪些部分将成为constexpr? [英] Which parts of the C++14 Standard Library could be and which parts will be made constexpr?

查看:269
本文介绍了C ++ 14标准库的哪些部分可以和哪些部分将成为constexpr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用新的 relaxed C ++ 14 constexpr规则 ,编译时编程变得更加富有表现力。我不知道标准图书馆是否也会升级以利用。特别是 std :: initializer_list std :: pair std :: tuple std :: complex std :: bitset std :: array 似乎是标记为 constexpr 批发的主要候选者。



问题




  • 标准库现在标记为 constexpr

  • 可以哪些其他部分标记为 constexpr

  • 例如。为什么不是< cmath> < algorithm> 的函数标记为 constexpr

  • 是否有向后兼容性原因?


解决方案

从草案中,我查看了C ++ 14, N3690 ,以下将更改为 constexpr 到目前为止(与C ++ 11标准比较)†:




  • std :: error_category 的默认构造函数

  • std ::向前

  • std :: move

  • std :: move_if_noexcept

  • 所有 std :: pair li>
  • std :: get std :: pair std :: make_tuple


  • std :: tuple 的所有操作符比较

  • std :: optional 的运算符比较

  • std :: optional 的所有构造函数>
  • 运算符[] 大小 for std :: bitset 和其他容器。

  • std :: complex 的所有操作符比较



†因为我是手动的,你可以预期一些错误:(



另一个可能更正确的列表 constexpr 添加项: N3469 N3470 N3471


其他部分可以标记为constexpr?


大多数可能 constexpr std :: numeric_limits 评估, std :: tuple std :: pair 构造函数等)在C ++ 11标准中已经被标记为 constexpr 。有一个错误,其中 std :: ratio 的时间点和其他组件未标记为 constexpr ,但在N3469中已修复。



constexpr 添加将会受益于 std :: initializer_list ,这没有得到任何这个时间(和我不确定是否有任何建议允许它)。


有没有向后兼容性的原因?


由于这是一个扩展,大多数的东西不会被打破,因为旧的代码仍然会按原样编译,没有什么是现在的错误。但是,如果你没有期望它,将 constexpr 添加到没有它的旧的东西可能会导致一些令人惊讶的结果,例如在此提供(感谢TemplateRex)


With the new relaxed C++14 constexpr rules, compile-time programming becomes a lot more expressive. 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.

Questions:

  • which parts of the Standard Library will now be marked constexpr?
  • which other parts could be marked constexpr?
  • e.g. why aren't the functions from <cmath> and <algorithm> marked constexpr?
  • are there backwards compatibility reasons not to do so?

解决方案

which parts of the Standard Library will now be marked 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)†:

  • std::error_category's default constructor
  • std::forward
  • std::move
  • std::move_if_noexcept
  • All of std::pair's operator comparisons
  • std::get for std::pair and std::tuple.
  • std::make_tuple
  • All of std::tuple's operator comparisons
  • All of std::optional's operator comparisons
  • All of std::optional's constructors (save for move)
  • operator[] and size for std::bitset and other containers.
  • All of std::complex's operator comparisons

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

which other parts could be marked 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. 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.

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

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)

这篇关于C ++ 14标准库的哪些部分可以和哪些部分将成为constexpr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆