std::array initialization

前端 未结 1 1987
北恋
北恋 2020-12-17 17:28

A std::array is essentially a C-style array wrapped in a struct. The initialization of structs requires braces, and the initi

相关标签:
1条回答
  • 2020-12-17 17:37

    The benefit is that you have ... less to type. But the drawback is that you are only allowed to leave off braces when the declaration has that form. If you leave off the =, or if the array is a member and you initialize it with member{{1, 2, 3, 4, 5}}, you cannot only pass one pair of braces.

    This is because there were worries of possible overload ambiguities when braces are passed to functions, as in f({{1, 2, 3, 4, 5}}). But it caused some discussion and an issue report has been generated.

    Essentially, the = { ... } initialization always has been able to omit braces, as in

    int a[][2] = { 1, 2, 3, 4 };
    

    That's not new. What is new is that you can omit the =, but then you must specify all braces

    int a[][2]{ {1, 2}, {3, 4} };
    
    0 讨论(0)
提交回复
热议问题