Why is this considered an extended initializer list?

后端 未结 2 748
天涯浪人
天涯浪人 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<foo> v(1);
        v[0] = null_foo;
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-17 17:06

    Brace-initialization for aggregates is only valid during declaration-initialization:

    Foo a = { 1, 2, 3 };
    

    It is not a way to generate temporaries midway: some_function(true, {1,2,3}, 'c').

    C++11 adds uniform initialization in which you can indeed write f(Foo{1,2,3});.

    0 讨论(0)
提交回复
热议问题