<legend id='oW2vr'><style id='oW2vr'><dir id='oW2vr'><q id='oW2vr'></q></dir></style></legend>
      1. <small id='oW2vr'></small><noframes id='oW2vr'>

        <tfoot id='oW2vr'></tfoot>
        <i id='oW2vr'><tr id='oW2vr'><dt id='oW2vr'><q id='oW2vr'><span id='oW2vr'><b id='oW2vr'><form id='oW2vr'><ins id='oW2vr'></ins><ul id='oW2vr'></ul><sub id='oW2vr'></sub></form><legend id='oW2vr'></legend><bdo id='oW2vr'><pre id='oW2vr'><center id='oW2vr'></center></pre></bdo></b><th id='oW2vr'></th></span></q></dt></tr></i><div id='oW2vr'><tfoot id='oW2vr'></tfoot><dl id='oW2vr'><fieldset id='oW2vr'></fieldset></dl></div>
        • <bdo id='oW2vr'></bdo><ul id='oW2vr'></ul>

        为什么 std::vector 和 std::array 的 C++ initializer_list 行为不同?

        时间:2024-05-12

          <small id='DbbuX'></small><noframes id='DbbuX'>

          <tfoot id='DbbuX'></tfoot>
            <tbody id='DbbuX'></tbody>

            <i id='DbbuX'><tr id='DbbuX'><dt id='DbbuX'><q id='DbbuX'><span id='DbbuX'><b id='DbbuX'><form id='DbbuX'><ins id='DbbuX'></ins><ul id='DbbuX'></ul><sub id='DbbuX'></sub></form><legend id='DbbuX'></legend><bdo id='DbbuX'><pre id='DbbuX'><center id='DbbuX'></center></pre></bdo></b><th id='DbbuX'></th></span></q></dt></tr></i><div id='DbbuX'><tfoot id='DbbuX'></tfoot><dl id='DbbuX'><fieldset id='DbbuX'></fieldset></dl></div>
              <legend id='DbbuX'><style id='DbbuX'><dir id='DbbuX'><q id='DbbuX'></q></dir></style></legend>
              • <bdo id='DbbuX'></bdo><ul id='DbbuX'></ul>
                • 本文介绍了为什么 std::vector 和 std::array 的 C++ initializer_list 行为不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  代码:

                  std::vector<int> x{1,2,3,4};
                  std::array<int, 4> y{{1,2,3,4}};
                  

                  为什么 std::array 需要双花括号?

                  Why do I need double curly braces for std::array?

                  推荐答案

                  std::array 是一个聚合:它没有任何用户声明的构造函数,甚至没有一个采用 std::initializer_list.使用大括号进行初始化是使用聚合初始化来执行的,这是从 C 继承的 C++ 特性.

                  std::array<T, N> is an aggregate: it doesn't have any user-declared constructors, not even one taking a std::initializer_list. Initialization using braces is performed using aggregate initialization, a feature of C++ that was inherited from C.

                  聚合初始化的旧风格"使用=:

                  The "old style" of aggregate initialization uses the =:

                  std::array<int, 4> y = { { 1, 2, 3, 4 } };
                  

                  使用这种老式的聚合初始化,额外的大括号可能会被省略,所以这相当于:

                  With this old style of aggregate initialization, extra braces may be elided, so this is equivalent to:

                  std::array<int, 4> y = { 1, 2, 3, 4 };
                  

                  然而,这些额外的大括号只能在T x = { a };"形式的声明中被省略(C++11 §8.5.1/11),也就是说,当使用旧样式 = 时.这条允许大括号省略的规则不适用于直接列表初始化.这里的脚注是:在列表初始化的其他用途中不能省略大括号."

                  However, these extra braces may only be elided "in a declaration of the form T x = { a };" (C++11 §8.5.1/11), that is, when the old style = is used . This rule allowing brace elision does not apply for direct list initialization. A footnote here reads: "Braces cannot be elided in other uses of list-initialization."

                  有关于此限制的缺陷报告:CWG 缺陷 #1270.如果提议的决议获得通过,则其他形式的列表初始化将允许省略大括号,以下内容将是良构的:

                  There is a defect report concerning this restriction: CWG defect #1270. If the proposed resolution is adopted, brace elision will be allowed for other forms of list initialization, and the following will be well-formed:

                  std::array<int, 4> y{ 1, 2, 3, 4 };
                  

                  (感谢 Ville Voutilainen 找到缺陷报告.)

                  (Hat tip to Ville Voutilainen for finding the defect report.)

                  这篇关于为什么 std::vector 和 std::array 的 C++ initializer_list 行为不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么在函数上使用函子? 下一篇:通过继承扩展 C++ 标准库?

                  相关文章

                  <i id='HmhqT'><tr id='HmhqT'><dt id='HmhqT'><q id='HmhqT'><span id='HmhqT'><b id='HmhqT'><form id='HmhqT'><ins id='HmhqT'></ins><ul id='HmhqT'></ul><sub id='HmhqT'></sub></form><legend id='HmhqT'></legend><bdo id='HmhqT'><pre id='HmhqT'><center id='HmhqT'></center></pre></bdo></b><th id='HmhqT'></th></span></q></dt></tr></i><div id='HmhqT'><tfoot id='HmhqT'></tfoot><dl id='HmhqT'><fieldset id='HmhqT'></fieldset></dl></div>
                  • <bdo id='HmhqT'></bdo><ul id='HmhqT'></ul>

                  1. <tfoot id='HmhqT'></tfoot>

                  2. <legend id='HmhqT'><style id='HmhqT'><dir id='HmhqT'><q id='HmhqT'></q></dir></style></legend>

                    1. <small id='HmhqT'></small><noframes id='HmhqT'>