1. <legend id='DjQai'><style id='DjQai'><dir id='DjQai'><q id='DjQai'></q></dir></style></legend>
    2. <i id='DjQai'><tr id='DjQai'><dt id='DjQai'><q id='DjQai'><span id='DjQai'><b id='DjQai'><form id='DjQai'><ins id='DjQai'></ins><ul id='DjQai'></ul><sub id='DjQai'></sub></form><legend id='DjQai'></legend><bdo id='DjQai'><pre id='DjQai'><center id='DjQai'></center></pre></bdo></b><th id='DjQai'></th></span></q></dt></tr></i><div id='DjQai'><tfoot id='DjQai'></tfoot><dl id='DjQai'><fieldset id='DjQai'></fieldset></dl></div>

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

      <tfoot id='DjQai'></tfoot>
        <bdo id='DjQai'></bdo><ul id='DjQai'></ul>

      1. 函数调用中的单元素向量初始化

        时间:2023-06-05
      2. <small id='3i14K'></small><noframes id='3i14K'>

          <bdo id='3i14K'></bdo><ul id='3i14K'></ul>

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

            • <legend id='3i14K'><style id='3i14K'><dir id='3i14K'><q id='3i14K'></q></dir></style></legend>
                  本文介绍了函数调用中的单元素向量初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  考虑以下示例代码:

                  示例:

                  void print(int n) {
                      cout << "element print
                  ";
                  }
                  
                  void print(vector<int> vec) {
                      cout << "vector print
                  ";
                  }
                  
                  int main() {
                     /* call 1 */ print(2);
                     /* call 2 */ print({2});
                     std::vector<int> v = {2};
                     /* call 3 */ print(v);
                     /* call 4 */ print( std::vector<int>{2} );
                     return 0;
                  }
                  

                  生成以下输出:

                  element print
                  element print
                  vector print
                  vector print
                  

                  为什么对 print 函数的调用(上例中的调用 2)与接受单个值的函数匹配?我在这个调用中创建了一个向量(包含一个元素),所以它应该不匹配以向量作为输入调用 print 吗?

                  Why the call to print function (call 2 in above example) is getting matched to function accepting a single value? I am creating a vector (containing a single element) in this call, so should it not match to call to print with vector as input?

                  这是部分讨论的在另一个问题中提供的解决方案适用于具有 1 个以上元素的向量.

                  This is partially discussed in an other question where the provided solution works for vectors with more than 1 elements.

                  推荐答案

                  因为第一个重载在 print({2}); 的重载决议中获胜.

                  Because the 1st overload wins in the overload resolution for print({2});.

                  在这两种情况下复制列表初始化都适用,对于第一个重载<代码>int,

                  In both cases copy list initialization applies, for the 1st overload taking int,

                  (强调我的)

                  否则(如果 T 不是类类型),如果花括号初始化列表只有一个元素并且 T 不是'不是引用类型或者是与元素类型兼容的引用类型,T 是直接初始化(在直接列表初始化中)或复制初始化(在复制列表中)初始化),除了不允许缩小转换.

                  Otherwise (if T is not a class type), if the braced-init-list has only one element and either T isn't a reference type or is a reference type that is compatible with the type of the element, T is direct-initialized (in direct-list-initialization) or copy-initialized (in copy-list-initialization), except that narrowing conversions are not allowed.

                  {2} 只有一个元素,可以直接初始化一个int作为参数;这是完全匹配.

                  {2} has only one element, it could be used to initialize an int as the argument directly; this is an exact match.

                  对于采用 std::vector 的第二次重载,

                  For the 2nd overload taking std::vector<int>,

                  否则,分两个阶段考虑T的构造函数:

                  Otherwise, the constructors of T are considered, in two phases:

                  • 所有将 std::initializer_list 作为唯一参数或作为第一个参数(如果其余参数具有默认值)的构造函数都被检查,并通过重载决议与类型的单个参数进行匹配std::initializer_list
                  • All constructors that take std::initializer_list as the only argument, or as the first argument if the remaining arguments have default values, are examined, and matched by overload resolution against a single argument of type std::initializer_list

                  这意味着 std::initializer_list 被构造并用作 std::vector 的构造函数参数(构造 std::vector 的参数代码>打印).需要一个用户定义的转换(通过 std::vector 的构造函数获取一个 std::initializer_list),那么它比第一个重载更糟糕.

                  That means an std::initializer_list<int> is constructed and used as the constructor's argument of std::vector<int> (to construct the argument for print). One user-defined conversion (via the constructor of std::vector taking one std::initializer_list) is required, then it's worse match than the 1st overload.

                  这篇关于函数调用中的单元素向量初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将元素添加到抽象类的 std::vector 下一篇:缩小向量

                  相关文章

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

                1. <legend id='yMHfH'><style id='yMHfH'><dir id='yMHfH'><q id='yMHfH'></q></dir></style></legend>

                    1. <tfoot id='yMHfH'></tfoot>
                        <bdo id='yMHfH'></bdo><ul id='yMHfH'></ul>

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