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

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

    • <bdo id='HdIab'></bdo><ul id='HdIab'></ul>
  1. <tfoot id='HdIab'></tfoot>

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

      不可复制的对象和值初始化:g++ 与 msvc

      时间:2023-10-06

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

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

              <bdo id='JOIV6'></bdo><ul id='JOIV6'></ul>
                本文介绍了不可复制的对象和值初始化: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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                相关文章

                最新文章

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

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