<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 行为不同?

        Why is the C++ initializer_list behavior for std::vector and std::array different?(为什么 std::vector 和 std::array 的 C++ initializer_list 行为不同?)

          <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 行为不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                  vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                  C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                  Adding types to the std namespace(将类型添加到 std 命名空间)
                  Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                  How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)
                    <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>

                        <tbody id='HmhqT'></tbody>

                        • <tfoot id='HmhqT'></tfoot>

                        • <legend id='HmhqT'><style id='HmhqT'><dir id='HmhqT'><q id='HmhqT'></q></dir></style></legend>

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