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

  • <legend id='cculw'><style id='cculw'><dir id='cculw'><q id='cculw'></q></dir></style></legend>
        <tfoot id='cculw'></tfoot>
      1. 初始化列表*参数*评估顺序

        Initializer list *argument* evaluation order(初始化列表*参数*评估顺序)
          <bdo id='8tfU4'></bdo><ul id='8tfU4'></ul>

              <tbody id='8tfU4'></tbody>
          1. <legend id='8tfU4'><style id='8tfU4'><dir id='8tfU4'><q id='8tfU4'></q></dir></style></legend>

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

              <small id='8tfU4'></small><noframes id='8tfU4'>

                  <tfoot id='8tfU4'></tfoot>
                  本文介绍了初始化列表*参数*评估顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  因此,C++ 标准要求类成员按照它们在类中声明的顺序进行初始化,而不是按照它们在任何构造函数的初始化列表中提到的顺序进行初始化.但是,这并不意味着对这些初始化的参数进行评估的顺序有任何关系.我正在使用一个经常传递对序列化对象的引用的系统,我想知道是否可以确保以正确的顺序从中读取位,而与这些位写入对象字段的顺序无关.

                  So, the C++ standard requires that class members be initialized in the order in which they are declared in the class, rather than the order that they're mentioned in any constructor's initializer list. However, this doesn't imply anything about the order in which the arguments to those initializations are evaluated. I'm working with a system that frequently passes references to serialization objects around, and wondering if I can ensure that bits are read from it in the right order, independent of the order in which those bits get written into the object's fields.

                  struct Foo {
                      int a;
                      double b;
                      // I want to be able to do this
                      Foo(SerObj &s)
                      : b(s.readDouble()), a(s.readInt())
                      { }
                      // Rather than this
                      Foo (SerObj &s)
                      {
                          b = s.readDouble();
                          a = s.readInt();
                      }
                  };
                  

                  显然,在声明中对 intsdoubles 之类的东西重新排序并不是什么大不了的事,但有时需要动态分配的更大的对象和东西可能会.

                  Obviously, reordering things like ints and doubles in the declaration is not too big a deal, but bigger objects and things requiring dynamic allocation sometimes can be.

                  推荐答案

                  C++ Standard 12.6.2/3:

                  C++ Standard 12.6.2/3:

                  每个base和member初始化后都有一个序列点(1.9).内存初始化器的表达式列表作为相应基类或成员初始化的一部分进行评估.

                  There is a sequence point (1.9) after the initialization of each base and member. The expression-list of a mem-initializer is evaluated as part of the initialization of the corresponding base or member.

                  初始化的顺序是您在问题中指定的顺序.评估是这个初始化的一部分,初始化不能交错(因为它们之间有一个序列点).

                  The order of the initialization is the one you specified in the question. Evaluation is part of this initialization, and the initializations can't interleave (because there is a sequence point between them).

                  这意味着初始化列表中的函数调用不是按所需顺序调用的,而是按成员声明出现的顺序调用的.

                  That means that the function calls in your initializer lists are not called in the desired order, but in the order in which the member declarations appear.

                  这篇关于初始化列表*参数*评估顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

                  <legend id='3Fya4'><style id='3Fya4'><dir id='3Fya4'><q id='3Fya4'></q></dir></style></legend><tfoot id='3Fya4'></tfoot>

                      <tbody id='3Fya4'></tbody>
                    • <small id='3Fya4'></small><noframes id='3Fya4'>

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