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

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

    2. <small id='dldf9'></small><noframes id='dldf9'>

        <legend id='dldf9'><style id='dldf9'><dir id='dldf9'><q id='dldf9'></q></dir></style></legend>
      1. 不可复制的对象和值初始化:g++ 与 msvc

        时间:2023-07-02
        <tfoot id='t9T7t'></tfoot>

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

          <bdo id='t9T7t'></bdo><ul id='t9T7t'></ul>

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

                    <tbody id='t9T7t'></tbody>
                1. <legend id='t9T7t'><style id='t9T7t'><dir id='t9T7t'><q id='t9T7t'></q></dir></style></legend>
                  本文介绍了不可复制的对象和值初始化:g++ 与 msvc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我发现 g++ 和 msvc 在初始化不可复制对象的值方面有一些不同的行为.考虑一个不可复制的类:

                  I'm seeing some different behavior between g++ and msvc around value initializing non-copyable objects. Consider a class that is non-copyable:

                  class noncopyable_base
                  {
                  public:
                      noncopyable_base() {}
                  
                  private:
                      noncopyable_base(const noncopyable_base &);
                      noncopyable_base &operator=(const noncopyable_base &);
                  };
                  
                  class noncopyable : private noncopyable_base
                  {
                  public:
                      noncopyable() : x_(0) {}
                      noncopyable(int x) : x_(x) {}
                  
                  private:
                      int x_;
                  };
                  

                  以及使用值初始化的模板,以便即使类型为 POD 值也会获得已知值:

                  and a template that uses value initialization so that the value will get a known value even when the type is POD:

                  template <class T>
                  void doit()
                  {
                      T t = T();
                      ...
                  }
                  

                  并尝试将它们一起使用:

                  and trying to use those together:

                  doit<noncopyable>();
                  

                  从 VC++ 9.0 开始,这在 msvc 上运行良好,但在我测试过的每个 g++ 版本(包括版本 4.5.0)上都失败了,因为复制构造函数是私有的.

                  This works fine on msvc as of VC++ 9.0 but fails on every version of g++ I tested this with (including version 4.5.0) because the copy constructor is private.

                  两个问题:

                  1. 哪些行为符合标准?
                  2. 关于如何在 gcc 中解决此问题的任何建议(并且明确地说,将其更改为 T t; 是不可接受的解决方案,因为这会破坏 POD 类型).
                  1. Which behavior is standards compliant?
                  2. Any suggestion of how to work around this in gcc (and to be clear, changing that to T t; is not an acceptable solution as this breaks POD types).

                  附言我看到 boost::noncopyable 也有同样的问题.

                  P.S. I see the same problem with boost::noncopyable.

                  推荐答案

                  您在 MSVC 中看到的行为是一个扩展,尽管它在下一页(重点是我的)中以迂回的方式进行了记录http://msdn.microsoft.com/en-us/library/0yw5843c.aspx:

                  The behavior you're seeing in MSVC is an extension, though it's documented as such in a roundabout way on the following page (emphasis mine) http://msdn.microsoft.com/en-us/library/0yw5843c.aspx:

                  等号初始化语法与函数式语法不同,尽管生成的代码在大多数情况下是相同的.不同之处在于,当使用等号语法时,编译器的行为就好像发生了以下事件序列:

                  The equal-sign initialization syntax is different from the function-style syntax, even though the generated code is identical in most cases. The difference is that when the equal-sign syntax is used, the compiler has to behave as if the following sequence of events were taking place:

                  • 创建一个与正在初始化的对象类型相同的临时对象.
                  • 将临时对象复制到对象中.

                  在编译器执行这些步骤之前,构造函数必须是可访问的.尽管编译器在大多数情况下可以消除临时创建和复制步骤,但不可访问的复制构造函数会导致等号初始化失败(在/Za、/Ze(禁用语言扩展)下).

                  The constructor must be accessible before the compiler can perform these steps. Even though the compiler can eliminate the temporary creation and copy steps in most cases, an inaccessible copy constructor causes equal-sign initialization to fail (under /Za, /Ze (Disable Language Extensions)).

                  参见 Ben Voigt 的回答 解决方法是 boost::value_initialized,正如 litb 在对 Ben 的回答的评论中指出的那样.boost::value_initalized 的文档对问题、解决方法和各种编译器问题的一些陷阱进行了很好的讨论.

                  See Ben Voigt's answer for a workaround which is a simplified version of boost::value_initialized, as pointed out by litb in a comment to Ben's answer. The docs for boost::value_initalized has a great discussion of the problem, the workaround, and some of the pitfalls of various compiler issues.

                  这篇关于不可复制的对象和值初始化:g++ 与 msvc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:MFC:更改 CEdit 的颜色 下一篇:“第一次机会例外"在 MFC 应用程序中的含义?

                  相关文章

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

                      <small id='3jqHx'></small><noframes id='3jqHx'>

                      <tfoot id='3jqHx'></tfoot>