简体   繁体   English

从静态constexpr初始化向量时,C ++未知类型

[英]C++ unknown type when initialise vector from static constexpr

C++ gcc compiler gives unclear error when compile: C ++ gcc编译器在编译时给出不清楚的错误:

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

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

int main()
{
  return 0;
}

I compile with: g++ var_test.cc -o var_test -std=c++0x Result: 我使用以下命令进行编译:g ++ var_test.cc -o var_test -std = c ++ 0x结果:

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

Why is it unknown? 为什么未知? C++0x should have in place member initialization C ++ 0x应该具有就地成员初始化

C++11 does support in-place non-static member initialization, but to use it you need a brace-or-equal-initializer , ie one of these forms: C ++ 11确实支持就地非静态成员初始化,但是要使用它,您需要使用brace-or-equal-initializer ,即以下形式之一:

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

Using parentheses isn't supported as it looks too much like a function declaration. 不支持使用括号,因为它看起来很像一个函数声明。

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

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