- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
编辑2:
澄清:
这个问题源于一个问题,该问题实际上与默认参数本身无关,但与复制构造函数和移动构造函数无关。
我接受了实际上可以回答问题的答案(因此,如果您因为问题标题而在这里,请阅读它),并会解释为什么最初对我不起作用。
怎么了
因此,如“EDIT:”中所述的问题实际上非常简单:
对包含std::vector<std::unique_ptr<T>>
的类的分配将中断VisualStudio 2013中的编译(未经其他版本测试),并且错误消息非常隐晦。
评论中的假设是VC编译器存在一个错误,并试图调用一个不存在的拷贝构造函数。
该死,现在怎么办?
这个假设实际上是正确的,但在我最初理解的意义上并不是这样。
实际上,VCC实际上确实尝试调用隐式定义的MyClass
的move构造函数。但是,这就是问题所在,它没有正确定义它:
当明确定义移动构造函数MyClass(MyClass && a)
时,我们实际上可以通过编写如下代码来模仿编译器的行为:
MyClass(MyClass && a)
: foos_(a.foos_)
{}
使用此代码会生成与使用隐式定义完全相同的错误消息,我想
foos_
的拷贝构造函数,这当然是不可能的,因为它又不能为其内容调用拷贝构造函数,因为它们的类型为
std::unique_ptr
由于明显的原因,它没有复制构造函数。
MyClass(MyClass && a)
: foos_(std::move(a.foos_))
{}
一切工作都很好,因为现在调用了
std::vector
的move构造函数,从而调用了其内容的move构造函数。
std::vector
模板的实际类型,而只关心类本身,而该类本身确实定义了一个拷贝
constructor, so the VCC tries to use it, which fails because of the missing copy constructor in
std::unique_ptr `。
.cpp
:
// ConsoleApplication2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <vector>
#include <memory>
class Foo { };
class MyClass
{
public:
MyClass(std::vector<std::unique_ptr<Foo>> foos) :
foos_(std::move(foos))
{};
std::vector<std::unique_ptr<Foo>> foos_;
};
int _tmain(int argc, _TCHAR* argv[])
{
auto test = MyClass(std::vector<std::unique_ptr<Foo>>()); //remove this, and all works fine!
return 0;
}
尝试对其进行编译将导致以下错误(肯定适用于gcc!):
1> ConsoleApplication2.cpp
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(593): error C2280: 'std::unique_ptr<Foo,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' : attempting to reference a deleted function
1> with
1> [
1> _Ty=Foo
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(1486) : see declaration of 'std::unique_ptr<Foo,std::default_delete<_Ty>>::unique_ptr'
1> with
1> [
1> _Ty=Foo
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(592) : while compiling class template member function 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)'
1> with
1> [
1> _Ty=std::unique_ptr<Foo,std::default_delete<Foo>>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(723) : see reference to function template instantiation 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)' being compiled
1> with
1> [
1> _Ty=std::unique_ptr<Foo,std::default_delete<Foo>>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\type_traits(572) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1> with
1> [
1> _Ty=std::unique_ptr<Foo,std::default_delete<Foo>>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\vector(650) : see reference to class template instantiation 'std::is_empty<_Alloc>' being compiled
1> with
1> [
1> _Alloc=std::allocator<std::unique_ptr<Foo,std::default_delete<Foo>>>
1> ]
1> c:\users\felix\source\repos\infinite whitewursht\infinitewhitewursht\consoleapplication2\consoleapplication2.cpp(18) : see reference to class template instantiation 'std::vector<std::unique_ptr<Foo,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>' being compiled
1> with
1> [
1> _Ty=Foo
1> ]
MyClass(vector<unique_ptr<Foo>> foos) :
foos_(std::move(foos))
{};
通过这种简单的设置,一切都可以正常编译。
MyClass(vector<unique_ptr<Foo>>);
这样的构造函数的调用成功,并且行为符合预期。但我想将
foos
作为默认参数。
foos
的默认值?
MyClass(vector<unique_ptr<Foo>> foos = vector<unique_ptr<Foo>>()) :
foos_(std::move(foos))
{};
但不幸的是,这不起作用。我不知道为什么,如果有人可以对此有所阐明,那将是很好的。
MyClass() :
foos_() //or foos_(vector<unique_ptr<Foo>>())
{};
也不要工作。这两种方法都会导致来自编译器的错误消息和冗长的输出,其中最有趣的部分是:
c:\users\ username \source\repos\myProject\myProject\MyClass.h(47) : see reference to class template instantiation
MyClass
中实际 vector 定义的行号:
vector<unique_ptr<GameObject>> foos_;
因此,我的猜测是这确实与我在初始化中犯了一个严重错误有关。
GameObject.cpp
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(593): 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
1> with
1> [
1> _Ty=int
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(1486) : see declaration of 'std::unique_ptr<int,std::default_delete<_Ty>>::unique_ptr'
1> with
1> [
1> _Ty=int
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(592) : while compiling class template member function 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)'
1> with
1> [
1> _Ty=std::unique_ptr<int,std::default_delete<int>>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(723) : see reference to function template instantiation 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)' being compiled
1> with
1> [
1> _Ty=std::unique_ptr<int,std::default_delete<int>>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\type_traits(572) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1> with
1> [
1> _Ty=std::unique_ptr<int,std::default_delete<int>>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\vector(650) : see reference to class template instantiation 'std::is_empty<_Alloc>' being compiled
1> with
1> [
1> _Alloc=std::allocator<std::unique_ptr<int,std::default_delete<int>>>
1> ]
1> c:\users\felix\source\repos\infinite whitewursht\infinitewhitewursht\infinitewhitewursht\gameobject.h(47) : see reference to class template instantiation 'std::vector<std::unique_ptr<int,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>' being compiled
1> with
1> [
1> _Ty=int
1> ]
最佳答案
编写构造函数重载,它根本不需要 vector ,并且默认情况下会初始化 vector (为空 vector ):
MyClass() : foos_{}
{}
关于c++ - 如何使用std::vector <unique_ptr <T >>作为默认参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23281405/
我收到此错误消息: .rvm/gems/ruby-2.5.1/bin/ruby_executable_hooks:24:in `' 我重新安装了 Ruby rvm reinstall ruby-2.5
我开始从事 WPF Ribbon 开发,非常好! 我的问题是找到(免费)基本图标(如文件保存/打开/等,剪切/粘贴/等)。 你有什么建议吗? 最佳答案 你看过Visual Studio Icon Li
我只找到经典的声音ID,但我需要Chord(默认)日历警报。如何播放声音? ks #define systemSoundID 1315 AudioServicesPlaySystemSound (s
在 Magento 中创建货件时,有一个复选框可让您“通过电子邮件发送货件副本”。 默认情况下未选中。有谁知道我需要编辑哪个文件才能默认设置为“选中”? 最佳答案 这是一个app/design/adm
我有一个简单的 IValueConverter,它只使用 TypeConverter 进行转换。但是,在某些情况下,提供的 TypeConverter 会失败。 如果转换器未提供 Binding,我想
我正在阅读教程,默认 Activity 是一个扩展另一个类的类,它所拥有的只是一个覆盖方法。应用程序如何工作,因为它不做任何其他事情?我很困惑! 最佳答案 父类 Activity 为您处理一切。 关于
我刚刚开始研究游戏框架。我正在尝试构建 rest api,并将 postgresql 用于我的数据库连接。这是我第一次同时使用 play 和 postgre。我在 build.sbt 中建立了一个数据
是否可以创建具有以下属性的 python 对象: class Foo: def __default_method__(x): return x f = Foo() f(10) > 10
我是 jQuery 的新手,遇到了一个烦人的问题。我有一些登录字段,当该字段为空时会填充默认文本,然后在单击时删除。 我的问题是,当用户保存了他们的用户名/密码(使用浏览器)时,如果他们返回页面,登录
考虑这个代码片段: void Foo(std::string str1, std::string str2) {} template void Bar() { Foo(Types{}...);
我正在编写一个简单的 C 程序,我应该用缓冲区溢出来攻击它。所以,我不想在编译时使用任何标志。如何消除使用的默认标志? # readelf -p .GCC.command.line stack Str
考虑这个代码片段: void Foo(std::string str1, std::string str2) {} template void Bar() { Foo(Types{}...);
我有以下代码[这是一道面试题]: #include #include using namespace std; class A{ public: A(){ cout co
我想在 Autofac 中为每个匹配的生命周期范围注册创建一个实例,但偶尔需要从全局容器(没有匹配的生命周期范围)请求一个实例。在不存在匹配生命周期范围的情况下,我想给出一个顶级实例而不是抛出异常。
我正在做一个收集单词共现的修改版本,所以我编写了自己的 javascript,我正在跟踪三个对象中的出现。但是,一旦对象变大(约 800 万、300 万和 172000),每 100000 个句子需要
我正在使用 pykalman 模块中的 KalmanFilter,我想知道它如何处理缺失的观察结果。根据文档: In real world systems, it is common to have
我有一个应用了 RenderTransform 的 Canvas ,如下所示: 谁能告诉我这些值是什么意思?我似乎无法找到用于解析这些值的转换器。 最佳答案 如 RenderTransform是 T
我是 Linux 的新手,现在使用 CentOS 6。我在这里使用 MySQL 工作台,每当我尝试添加新连接时,它都会询问我默认的 key 环密码。我真的不知道,这个密码是从哪里设置的,我之前没有设置
我在 Ubuntu 18.04 上工作。我没有定义 GL_GLEXT_PROTOTYPES .我使用 glXGetProcAddress 加载“核心”OpenGL 函数.我的申请链接到 /usr/li
我按照文档中的示例添加了对使用 asio 加载 HTTPS 站点的支持,这意味着我调用 ctx.set_default_verify_paths();使用系统默认路径来查找证书。 然而,我得到:una
我是一名优秀的程序员,十分优秀!