这是一个完整的程序,可以重现我的问题。
#include <vector>
#include <list>
#include <memory>
#include <utility>
int main()
{
std::vector<std::list<std::unique_ptr<int>>> v;
std::list<std::unique_ptr<int>> l;
l.push_back(std::make_unique<int>(0));
l.push_back(std::make_unique<int>(1));
v.push_back(std::move(l)); // error
}
在最后一行,编译器提示 std::unique_ptr 的 deleted 复制构造函数被引用。
由于我将列表移动到 vector 中,我假设不会对列表的元素调用任何复制构造函数。
为什么会这样?我将如何解决它?
我正在使用 MSVC 2017。
错误全文:
example.cpp
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/xmemory0(840): error C2280: 'std::unique_ptr<int,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function
with
[
_Ty=int
]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/memory(1857): note: see declaration of 'std::unique_ptr<int,std::default_delete<_Ty>>::unique_ptr'
with
[
_Ty=int
]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/xmemory0(959): note: see reference to function template instantiation 'void std::allocator<_Other>::construct<_Objty,const std::unique_ptr<int,std::default_delete<_Ty>>&>(_Objty *,const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' being compiled
with
[
_Other=std::_List_node<std::unique_ptr<int,std::default_delete<int>>,void *>,
_Objty=std::unique_ptr<int,std::default_delete<int>>,
_Ty=int
]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/xmemory0(959): note: see reference to function template instantiation 'void std::allocator<_Other>::construct<_Objty,const std::unique_ptr<int,std::default_delete<_Ty>>&>(_Objty *,const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' being compiled
with
[
_Other=std::_List_node<std::unique_ptr<int,std::default_delete<int>>,void *>,
_Objty=std::unique_ptr<int,std::default_delete<int>>,
_Ty=int
]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/xmemory0(1097): note: see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,const std::unique_ptr<int,std::default_delete<int>>&>(std::allocator<_Other> &,_Objty *,const std::unique_ptr<int,std::default_delete<int>> &)' being compiled
with
[
_Alloc=std::allocator<std::_List_node<std::unique_ptr<int,std::default_delete<int>>,void *>>,
_Ty=std::unique_ptr<int,std::default_delete<int>>,
_Other=std::_List_node<std::unique_ptr<int,std::default_delete<int>>,void *>,
_Objty=std::unique_ptr<int,std::default_delete<int>>
]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/xmemory0(1096): note: see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,const std::unique_ptr<int,std::default_delete<int>>&>(std::allocator<_Other> &,_Objty *,const std::unique_ptr<int,std::default_delete<int>> &)' being compiled
with
[
_Alloc=std::allocator<std::_List_node<std::unique_ptr<int,std::default_delete<int>>,void *>>,
_Ty=std::unique_ptr<int,std::default_delete<int>>,
_Other=std::_List_node<std::unique_ptr<int,std::default_delete<int>>,void *>,
_Objty=std::unique_ptr<int,std::default_delete<int>>
]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/list(853): note: see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Other>>::construct<_Ty,const std::unique_ptr<int,std::default_delete<int>>&>(_Ty *,const std::unique_ptr<int,std::default_delete<int>> &)' being compiled
with
[
_Other=std::_List_node<std::unique_ptr<int,std::default_delete<int>>,void *>,
_Ty=std::unique_ptr<int,std::default_delete<int>>
]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/list(851): note: see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Other>>::construct<_Ty,const std::unique_ptr<int,std::default_delete<int>>&>(_Ty *,const std::unique_ptr<int,std::default_delete<int>> &)' being compiled
with
[
_Other=std::_List_node<std::unique_ptr<int,std::default_delete<int>>,void *>,
_Ty=std::unique_ptr<int,std::default_delete<int>>
]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/list(1086): note: see reference to function template instantiation 'std::_List_node<std::unique_ptr<int,std::default_delete<_Ty>>,void *> *std::_List_buy<std::unique_ptr<_Ty,std::default_delete<_Ty>>,_Alloc>::_Buynode<const std::unique_ptr<_Ty,std::default_delete<_Ty>>&>(std::_List_node<std::unique_ptr<_Ty,std::default_delete<_Ty>>,void *> *,std::_List_node<std::unique_ptr<_Ty,std::default_delete<_Ty>>,void *> *,const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' being compiled
with
[
_Ty=int,
_Alloc=std::allocator<std::unique_ptr<int,std::default_delete<int>>>
]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/list(1085): note: see reference to function template instantiation 'std::_List_node<std::unique_ptr<int,std::default_delete<_Ty>>,void *> *std::_List_buy<std::unique_ptr<_Ty,std::default_delete<_Ty>>,_Alloc>::_Buynode<const std::unique_ptr<_Ty,std::default_delete<_Ty>>&>(std::_List_node<std::unique_ptr<_Ty,std::default_delete<_Ty>>,void *> *,std::_List_node<std::unique_ptr<_Ty,std::default_delete<_Ty>>,void *> *,const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' being compiled
with
[
_Ty=int,
_Alloc=std::allocator<std::unique_ptr<int,std::default_delete<int>>>
]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/list(1463): note: see reference to function template instantiation 'void std::list<std::unique_ptr<int,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>::_Insert<const std::unique_ptr<_Ty,std::default_delete<_Ty>>&>(std::_List_unchecked_const_iterator<std::_List_val<std::_List_simple_types<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>,std::_Iterator_base0>,const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' being compiled
with
[
_Ty=int
]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/list(1463): note: see reference to function template instantiation 'void std::list<std::unique_ptr<int,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>::_Insert<const std::unique_ptr<_Ty,std::default_delete<_Ty>>&>(std::_List_unchecked_const_iterator<std::_List_val<std::_List_simple_types<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>,std::_Iterator_base0>,const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' being compiled
with
[
_Ty=int
]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/list(1423): note: see reference to function template instantiation 'void std::list<std::unique_ptr<int,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>::_Insert_range<_Iter>(std::_List_unchecked_const_iterator<std::_List_val<std::_List_simple_types<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>,std::_Iterator_base0>,_Iter,_Iter,std::forward_iterator_tag)' being compiled
with
[
_Ty=int,
_Iter=std::_List_const_iterator<std::_List_val<std::_List_simple_types<std::unique_ptr<int,std::default_delete<int>>>>>
]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/list(1422): note: see reference to function template instantiation 'void std::list<std::unique_ptr<int,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>::_Insert_range<_Iter>(std::_List_unchecked_const_iterator<std::_List_val<std::_List_simple_types<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>,std::_Iterator_base0>,_Iter,_Iter,std::forward_iterator_tag)' being compiled
with
[
_Ty=int,
_Iter=std::_List_const_iterator<std::_List_val<std::_List_simple_types<std::unique_ptr<int,std::default_delete<int>>>>>
]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/list(939): note: see reference to function template instantiation 'std::_List_iterator<std::_List_val<std::_List_simple_types<std::unique_ptr<int,std::default_delete<_Ty>>>>> std::list<std::unique_ptr<_Ty,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>::insert<std::_List_const_iterator<std::_List_val<std::_List_simple_types<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>>>(std::_List_const_iterator<std::_List_val<std::_List_simple_types<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>>,_Iter,_Iter)' being compiled
with
[
_Ty=int,
_Iter=std::_List_const_iterator<std::_List_val<std::_List_simple_types<std::unique_ptr<int,std::default_delete<int>>>>>
]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/list(939): note: see reference to function template instantiation 'std::_List_iterator<std::_List_val<std::_List_simple_types<std::unique_ptr<int,std::default_delete<_Ty>>>>> std::list<std::unique_ptr<_Ty,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>::insert<std::_List_const_iterator<std::_List_val<std::_List_simple_types<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>>>(std::_List_const_iterator<std::_List_val<std::_List_simple_types<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>>,_Iter,_Iter)' being compiled
with
[
_Ty=int,
_Iter=std::_List_const_iterator<std::_List_val<std::_List_simple_types<std::unique_ptr<int,std::default_delete<int>>>>>
]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/list(935): note: while compiling class template member function 'std::list<std::unique_ptr<int,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>::list(const std::list<std::unique_ptr<_Ty,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>> &)'
with
[
_Ty=int
]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/xmemory0(840): note: see reference to function template instantiation 'std::list<std::unique_ptr<int,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>::list(const std::list<std::unique_ptr<_Ty,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>> &)' being compiled
with
[
_Ty=int
]
<source>(10): note: see reference to class template instantiation 'std::list<std::unique_ptr<int,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>' being compiled
with
[
_Ty=int
]
/opt/compiler-explorer/windows/19.10.25017/lib/native/include/memory(1857): note: 'std::unique_ptr<int,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': function was explicitly deleted
with
[
_Ty=int
]
Microsoft (R) C/C++ Optimizing Compiler Version 19.10.25017 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
Compiler exited with result code 2
最佳答案
Since I'm moving the list into the vector, I assume no copy constructors will be called on the elements of the list.
std::list的移动构造函数不是noexcept([list.overview]),所以std::vector需要调用它的复制构造函数以提供强大的异常保证。 std::list 的复制构造函数(不出所料)调用列表元素的复制构造函数。
显然 libstdc++ 将 std::list 的移动构造函数声明为 noexcept,这是标准允许的,但不是必需的。 ([res.on.exception.handling]/5)
关于c++ - 将 std::list<std::unique_ptr> 移动到 vector 中尝试引用已删除的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43235335/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我
我在一个我想在formtasticGem中覆盖的方法中找到了这个。该方法如下所示:defto_htmlinput_wrappingdohidden_field_html是什么意思?在第三行做什么?我知道它对数组有什么作用,但在这里我不知道。 最佳答案 你可以这样读:hidden_field_htmllabel_with_nested_checkbox是连接到hidden_field_html末尾的参数-为了“清晰”,他们将其分成两行 关于ruby-on-rails-没有参数的`
我已经看到了一些其他的问题,尝试了他们的建议,但没有一个对我有用。我已经使用Rails大约一年了,刚刚开始一个新的Rails项目,突然遇到了问题。我卸载并尝试重新安装所有Ruby和Rails。Ruby很好,但Rails不行。当我输入railss时,我得到了can'tfindgemrailties。我当前的Ruby版本是ruby2.2.2p95(2015-04-13修订版50295)[x86_64-darwin15],尽管我一直在尝试通过rbenv设置ruby2.3.0。如果我尝试rails-v查看我正在运行的版本,我会得到同样的错误。我使用的是MacOSXElCapitan版本10
考虑一下:现在这些情况:#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2我需要用其他字符串输出URL。我如何保证&符号不会被转义?由于我无法控制的原因,我无法发送&。求助!把我的头发拉到这里:\编辑:为了澄清,我实际上有一个像这样的数组:@images=[{:id=>"fooid",:url=>"http://
我正在处理http://prepwork.appacademy.io/mini-curriculum/array/中概述的数组问题我正在尝试创建函数my_transpose,它接受一个矩阵并返回其转置。我对写入二维数组感到很困惑!这是一个代码片段,突出了我的困惑。rows=[[0,1,2],[3,4,5],[6,7,8]]columns=Array.new(3,Array.new(3))putscolumns.to_s#Outputisa3x3arrayfilledwithnilcolumns[0][0]=0putscolumns.to_s#Outputis[[0,nil,nil],[