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

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

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

      2. 值初始化和非 POD 类型

        Value initialization and Non POD types(值初始化和非 POD 类型)
        <tfoot id='gn8gn'></tfoot>
      3. <small id='gn8gn'></small><noframes id='gn8gn'>

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

        <legend id='gn8gn'><style id='gn8gn'><dir id='gn8gn'><q id='gn8gn'></q></dir></style></legend>
          <bdo id='gn8gn'></bdo><ul id='gn8gn'></ul>

                • 本文介绍了值初始化和非 POD 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  一个小时前我发布了一个答案 这里 根据我的说法是正确的.但是我的回答被 Martin B 否决了.他说

                  <块引用>

                  你很幸运并且得到了零,因为我放入的内存恰好是零初始化的.标准不保证这一点.

                  但是在阅读 Michael Burr 的回答这里并尝试以下示例代码后>

                  1)

                  #include 结构 B { ~B();国际米;};int main(){B * b = 新 B();断言(b-> m == 0);}

                  我在 MSVC++ 2010 上遇到了调试错误.

                  当我尝试以下代码时遇到了类似的错误 [我的回答 此处] 在 MSVC++2010 上

                  2)

                  #include 结构体{std::string 字符串;int 整数;布尔k;//添加添加添加};结构指令:结构{结构():结构(){}};int main(){指令我;断言(i.k == 0);}

                  (1)(2) 都没有在 gcc/Clang 上给出任何这样的错误,这让我觉得 MSVC++2010 是否不支持 C++03.我不确定.

                  根据 Michael Burr 的帖子 [在 C++03 中]

                  <块引用>

                  new B() - 值初始化 B,它对所有字段进行零初始化,因为它的默认构造函数是编译器生成的,而不是用户定义的.

                  标准说

                  <块引用>

                  要对 Tmeans 类型的对象进行值初始化:

                  ——如果 T 是一个类类型(第 9 条),带有用户声明的构造函数(12.1),然后调用 T 的默认构造函数(如果没有可访问的默认构造函数);

                  .....

                  否则,对象被零初始化

                  从第一点开始,如果没有用户声明的默认构造函数,编译器合成的默认构造函数将被调用,它将零初始化所有字段(根据最后一点).

                  那么我错在哪里?我对值初始化的解释是否正确?

                  解决方案

                  Visual Studio 在所有当前版本(2005、2008、2010)中都存在已知错误,即它没有正确实现非 POD 类型的值初始化'没有用户声明的构造函数.

                  根据语言规则,你们断言都不应该触发,但确实会出现编译器问题.这些是一些错误报告,请注意它们都已关闭或已解决为无法修复".

                  http://connect.microsoft.com/VisualStudio/反馈/细节/564268/c-value-initialization

                  http://connect.microsoft.com/VisualStudio/feedback/details/484295/vc-does-not-value-initialize-members-of-derived-classes-without-user-declared-构造函数

                  http://connect.microsoft.com/VisualStudio/feedback/details/100744/value-initialization-in-new-expression

                  An hour ago I posted an answer here which according to me was correct. However my answer was downvoted by Martin B. He said

                  You're just lucky and are getting zeros because the memory that i was placed in happened to be zero-initialized. This is not guaranteed by the standard.

                  However after reading Michael Burr's answer here and trying the following sample code

                  1)

                  #include <cassert>
                  
                  struct B { ~B(); int m; };
                  
                  int main()
                  {
                     B * b = new B();
                     assert(b->m == 0);
                  }
                  

                  I got a debug error on MSVC++ 2010.

                  I got a similar error when I tried the following code [My answer here] on MSVC++2010

                  2)

                  #include <cassert>
                  struct Struct {
                      std::string String;
                      int Int;
                      bool k;
                      // add add add
                  };
                  
                  struct InStruct : Struct
                  {
                     InStruct() : Struct() {}
                  };
                  
                  int main()
                  {
                     InStruct i;
                     assert(i.k == 0);
                  }
                  

                  Neither (1) nor (2) gave any such error on gcc/Clang which made me think if MSVC++2010 does not support C++03. I am not sure.

                  According to Michael Burr's post [in C++03]

                  new B() - value-initializes B which zero-initializes all fields since its default ctor is compiler generated as opposed to user-defined.

                  The Standard says

                  To value-initialize an object of type Tmeans:

                  — if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if Thas no accessible default constructor);

                  .....

                  otherwise, the object is zero-initialized

                  From the first point if there is no user declared default constructor the compiler synthesized default constructor will be called which will zero initialize all the fields (according to last point).

                  So where am I wrong? Is my interpretation of value initialization correct?

                  解决方案

                  Visual Studio has known bugs in all current versions (2005, 2008, 2010) where it doesn't correctly implement value-initialization for non-POD types that don't have a user declared constructor.

                  By the language rules none of you asserts should fire but do exhibit the compiler issues. These are some of the bug reports, note that they are all closed or resolved as "Won't Fix".

                  http://connect.microsoft.com/VisualStudio/feedback/details/564268/c-value-initialization

                  http://connect.microsoft.com/VisualStudio/feedback/details/484295/vc-does-not-value-initialize-members-of-derived-classes-without-user-declared-constructor

                  http://connect.microsoft.com/VisualStudio/feedback/details/100744/value-initialization-in-new-expression

                  这篇关于值初始化和非 POD 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
                  • <bdo id='a0YCo'></bdo><ul id='a0YCo'></ul>

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

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

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

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