1. <small id='9eakP'></small><noframes id='9eakP'>

    <tfoot id='9eakP'></tfoot>
      <bdo id='9eakP'></bdo><ul id='9eakP'></ul>

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

      使用 GCC 4.7 从初始化列表初始化 unique_ptrs 的容器失败

      Initializing container of unique_ptrs from initializer list fails with GCC 4.7(使用 GCC 4.7 从初始化列表初始化 unique_ptrs 的容器失败)

    1. <small id='o0pge'></small><noframes id='o0pge'>

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

                <legend id='o0pge'><style id='o0pge'><dir id='o0pge'><q id='o0pge'></q></dir></style></legend><tfoot id='o0pge'></tfoot>
                本文介绍了使用 GCC 4.7 从初始化列表初始化 unique_ptrs 的容器失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试以与 Bjarne Stroustrup 的 C++11 常见问题:

                using namespace std;
                vector<unique_ptr<string>> vs { new string{"Doug"}, new string{"Adams"} }; // fails
                unique_ptr<string> ps { new string{"42"} }; // OK
                

                我看不出为什么这个语法会失败.这种初始化容器的方式有什么问题吗?
                编译器错误信息很大;我找到的相关部分如下:

                I can see no reason why this syntax should fail. Is there something wrong with this way of initializing the container?
                The compiler error message is huge; the relevant segment I find is below:

                /usr/lib/gcc-snapshot/lib/gcc/i686-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_construct.h:77:7: 错误:没有匹配的函数调用'std::unique_ptr>::unique_ptr(std::basic_string<char>&)'

                /usr/lib/gcc-snapshot/lib/gcc/i686-linux-gnu/4.7.0/../../../../include/c++/4.7.0 /bits/stl_construct.h:77:7: error: no matching function for call to 'std::unique_ptr<std::basic_string<char> >::unique_ptr(std::basic_string<char>&)'

                有什么办法可以解决这个错误?

                What is the way to fix this error ?

                推荐答案

                unique_ptr 的构造函数是 explicit.所以你不能用 from new string{"foo"} 隐式地创建一个.它需要类似于 unique_ptr{ new string{"foo"} }.

                unique_ptr's constructor is explicit. So you can't create one implicitly with from new string{"foo"}. It needs to be something like unique_ptr<string>{ new string{"foo"} }.

                导致我们这样做

                // not good
                vector<unique_ptr<string>> vs {
                    unique_ptr<string>{ new string{"Doug"} },
                    unique_ptr<string>{ new string{"Adams"} }
                };
                

                但是,如果其中一个构造函数失败,它可能会泄漏.使用 make_unique 更安全:

                However it may leak if one of the constructors fails. It's safer to use make_unique:

                // does not work
                vector<unique_ptr<string>> vs {
                     make_unique<string>("Doug"),
                     make_unique<string>("Adams")
                };
                

                但是... initializer_lists 总是执行复制,而 unique_ptrs 是不可复制的.这对于初始化列表来说真的很烦人.您可以破解它,或者通过调用emplace_back回退到初始化.

                But... initializer_lists always perform copies, and unique_ptrs are not copyable. This is something really annoying about initializer lists. You can hack around it, or fallback to initialization with calls to emplace_back.

                如果您实际上使用智能指针管理 string 并且不仅仅是示例,那么您可以做得更好:只需制作一个 vector.std::string 已经处理了它使用的资源.

                If you're actually managing strings with smart pointers and it's not just for the example, then you can do even better: just make a vector<string>. The std::string already handles the resources it uses.

                这篇关于使用 GCC 4.7 从初始化列表初始化 unique_ptrs 的容器失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                Is Type(::x); valid?(是类型(::x);有效的?)
                Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                • <legend id='dEdXr'><style id='dEdXr'><dir id='dEdXr'><q id='dEdXr'></q></dir></style></legend>

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

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

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