C++20 constexpr 容器将如何工作?

2022-01-24 00:00:00 containers c++ stl c++20 constexpr

作为 constexpr std::字符串constexpr std::vector 已被 C++20 接受,这些将如何使用?链接的论文在细节上非常短.我们是否需要指定特殊的 constexpr 分配器,使编译时字符串/向量与其正常等效项不兼容?

As constexpr std::string and constexpr std::vector have been accepted into C++20, how will these be used? The linked papers are very short on details. Do we need to specify special constexpr allocators, making compile-time strings/vectors incompatible with their normal equivalents?

推荐答案

这两篇论文严重依赖P0784,其中讨论了编译时分配的工作方式.

Those two papers depend heavily on P0784, which discusses how allocations at compile-time will work.

不完整的答案:

  • 只有 std::allocator 可以工作.
  • 所有分配都会被跟踪,并且必须在编译完成之前解除分配.这意味着您可以在编译时进行操作,但不能初始化 stringvector 变量以在运行时使用.(就我个人而言,我认为这个限制很有可能会在标准的未来版本中取消 - 但这只是我的看法.)
  • Only std::allocator will work.
  • All allocations are tracked, and must be deallocated before compilation is complete. This means that you can do manipulations at compile-time, but you can't initialize string and vector variables to be used at run-time. (Personally, I think there's a good chance that this restriction will be lifted in a future version of the standard - but that's just my opinion.)

相关文章