gpt4 book ai didi

从静态 constexpr 初始化 vector 时的 C++ 未知类型

转载 作者:行者123 更新时间:2023-11-28 00:08:52 28 4
gpt4 key购买 nike

C++ gcc 编译器在编译时给出不清楚的错误:

    #include <iostream>
#include <vector>
using namespace std;

class A
{
constexpr static int i = 10;
vector<int>m(i);
};

int main()
{
return 0;
}

我编译:g++ var_test.cc -o var_test -std=c++0x结果:

var_test.cc:8:16: error: unknown type name 'i'
vector<int>m(i);

为什么不为人知? C++0x 应该有适当的成员初始化

最佳答案

C++11 确实支持就地非静态成员初始化,但要使用它,您需要一个brace-or-equal-initializer,即以下形式之一:

vector<int> m {i};
vector<int> m = vector<int>(i);
vector<int> m = vector<int>{i};

不支持使用括号,因为它看起来太像函数声明了。

关于从静态 constexpr 初始化 vector 时的 C++ 未知类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34085721/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com