为什么这被认为是一个扩展的初始化列表? [英] Why is this considered an extended initializer list?

查看:471
本文介绍了为什么这被认为是一个扩展的初始化列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include< vector> 

struct foo {
int i;
int j;
int k;
};

int main(){
std :: vector< foo> v(1);
v [0] = {0,0,0};
return 0;
}



当使用g ++编译时,我得到以下警告:


警告:扩展初始化器列表只能使用-std = c ++ 0x或-std = gnu ++ 0x [默认启用]


据我所知,它只是一个普通的初始化列表。该结构是一个POD类型。



这是一个错误还是我缺少某些东西?

解决方案

Pre C ++ 11(和可能的C99)只能在创建时初始化POD,而不是在任意运行时点,这是你在这里尝试的(从初始化器列表中赋值)。

您可以使用null_foo:

  int main $ b {
const foo null_foo = {0,0,0};
std :: vector< foo> v(1);
v [0] = null_foo;
return 0;
}


#include <vector>

struct foo {
    int i;
    int j;
    int k;
};

int main() {
    std::vector<foo> v(1);
    v[0] = {0, 0, 0};
    return 0;
}

When compiling this using g++, I get the following warning:

warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]

As far as I can tell, though, it's just a normal initializer list. The struct is a POD type.

Is this a bug or am I missing something?

解决方案

Pre C++11 (and possibly C99) you can only initialize a POD at creation, not at arbitrary runtime points, which is what you're attempting here (assignment from an initializer list).

You can make a null_foo though:

int main()
{
    const foo null_foo = {0, 0, 0};
    std::vector<foo> v(1);
    v[0] = null_foo;
    return 0;
}

这篇关于为什么这被认为是一个扩展的初始化列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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