Why is this considered an extended initializer list?

后端 未结 2 750
天涯浪人
天涯浪人 2021-01-17 16:35
#include 

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

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


        
2条回答
  •  鱼传尺愫
    2021-01-17 17:06

    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 v(1);
        v[0] = null_foo;
        return 0;
    }
    

提交回复
热议问题