简体   繁体   English

C++20 中的 constexpr std::vector 和 constexpr std::string

[英]constexpr std::vector and constexpr std::string in C++20

In new c++20 std::vector and std::string have constexpr constructors (and other methods).在新的 c++20 中,std::vector 和 std::string 具有 constexpr 构造函数(和其他方法)。 I learned https://www.heise.de/developer/artikel/constexpr-std-vector-und-constexpr-std-string-in-C-20-4906108.html article that has examples of using constexpr versions of vector and string.我学习了https://www.heise.de/developer/artikel/constexpr-std-vector-und-constexpr-std-string-in-C-20-4906108.html文章,其中包含使用向量和 constexpr 版本的示例细绳。 But when i tried to test this code on https://godbolt.org with different compilers(last versions) - they totally all failed.但是,当我尝试使用不同的编译器(最新版本)在https://godbolt.org上测试此代码时,它们都完全失败了。

for example this code (with -std=c++20 key)例如这段代码(使用 -std=c++20 键)

int main()
{
    constexpr std::vector myVec {15, -5, 0, 5, 10}; 
}

Is this feature not support yet?这个功能还不支持吗? ...Or..how to use this? ...或者..如何使用这个?

To bring @Jovibor's comment to life - as stated in cppreference , no compiler currently supports constexpr std::vector or constexpr std::string .为了使@Jovibor 的评论栩栩如生——如cppreference中所述,目前没有编译器支持constexpr std::vectorconstexpr std::string You just have to wait a bit.你只需要稍等片刻。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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