C++20常量表达式向量和字符串不起作用 [英] C++20 constexpr vector and string not working

查看:0
本文介绍了C++20常量表达式向量和字符串不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建constexprstd::stringstd::vector对象时遇到奇怪的编译器错误:

#include <vector>
#include <string>

int main()
{
    constexpr std::string cs{ "hello" };
    constexpr std::vector cv{ 1, 2, 3 };
    return 0;
}

编译器报告";表达式必须具有常量值:

我错过了什么吗?我使用的是最新的Microsoft Visual Studio 2019版本:16.11.4,参考(https://en.cppreference.com/w/cpp/compiler_support)说明此编译器版本支持constexpr字符串和向量:

我还尝试了constexpr std::array,它确实有效。该问题是否与向量关联的动态内存分配有关?

推荐答案

您的程序实际上是格式错误的,尽管错误可能很难理解。constexprC++20中的分配支持是有限的-您只能进行临时分配。也就是说,必须在常量评估结束时完全释放分配。

所以您不能这样写:

int main() {
    constexpr std::vector<int> v = {1, 2, 3};
}

因为v的分配是持续的-它是非暂时性的。这就是错误告诉您的:

<source>(6): error C2131: expression did not evaluate to a constant
<source>(6): note: (sub-)object points to memory which was heap allocated during constant evaluation

v不能是常量,因为它仍在保留堆分配,而且不允许这样做。

但您可以写入this

constexpr int f() {
    std::vector<int> v = {1, 2, 3};
    return v.size();
}

static_assert(f() == 3);

在这里,v的分配是暂时的-f()返回时将释放内存。但我们仍然可以在constexpr期间使用std::vector

这篇关于C++20常量表达式向量和字符串不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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