move-constructor相关内容
我有以下代码: #include #include using std::cout; struct SomeType { SomeType() {} SomeType(const SomeType &&other) { cout
..
使用 Visual Studio 2015 可以成功编译以下代码,但使用 Visual Studio 2017 编译失败.Visual Studio 2017 报告: 错误 C2280:“std::pair::pair(const std::pair &)":试图引用已删除的函数 代码 #include #include 结构节点{std::un
..
以下代码片段导致在我期望调用移动构造函数的地方调用复制构造函数: #include 结构体{Foo() { puts("Foo 被构建了!");}Foo(const Foo& foo) { puts("Foo 被复制了!");}Foo(Foo&& foo) { puts("Foo 被移动了!");}};结构栏 { Foo foo;};酒吧喵(){酒吧酒吧;返回栏;}int main() { Ba
..
我想刷新我对编译器通常自动生成默认构造函数、复制构造函数和赋值运算符的条件的记忆. 我记得有一些规则,但我不记得了,而且在网上也找不到信誉良好的资源.有人可以帮忙吗? 解决方案 在下文中,“自动生成"的意思是“隐式声明为默认值,但未定义为已删除".有些情况下,特殊成员函数被声明,但被定义为删除. 如果没有用户声明的构造函数(第 12.1/5 节),则会自动生成默认构造函数.
..
我有以下设置: class MyClass {上市:静态MyClass Clone(const MyClass& other){返回MyClass(other,10);}静态MyClass CreateNormal(int x,int y){返回MyClass(int x,int y);}//还有更多的工厂功能.私人的://构造函数1MyClass(int x,int y):b_(x,y){}
..
简介 当类使用多重继承时,如何安全地设计move构造函数? 详细信息 请考虑以下情形: struct T {};struct U {};结构X:公共T,公共U{X(X&& other):T(std :: move(other)),U(std :: move(other))//已移动?!{}}; 是否可以安全地移动构造 T 和 U ? 解决方案 tl; dr :问题
..
请考虑以下课程.如果我自己按照以下方式实现move构造函数,为什么酒吧成员 b 不移动而是被复制?但是,如果我使用默认的move构造函数,那么 b 会被移动.为什么 b(rhs.b)不调用 bar(bar&&)吗? 我将g ++ 9.2.1与--std = c ++ 11一起使用. 类栏{上市:bar(){cout
..
以下代码可以使用Visual Studio 2015成功编译,但是使用Visual Studio 2017则失败.VisualStudio 2017报告: 错误C2280:"std :: pair :: pair(const std :: pair&)":试图引用已删除的函数 代码 #include#include结构节点{std
..
我有一个类,其中包含一个 std :: vector struct Mystruct { Mystruct(const std :: vector& w):v(w) { std :: cout& w):v(w) { std
..
我想检测(并在 std :: enable_if 中使用结果)是否C ++类定义了移动构造函数。 下面的程序打印 MOVE ,因此使用 std :: is_move_constructible 是不可行的要做到这一点: #include #include C类{ public: C(){puts(“ C()”); } C
..
尝试编译以下代码: struct Foo { 显式Foo(void) {} 明确的Foo(Foo&&rhs){} }; Foo bar(void) { return Foo(); } 出现以下错误: 调用隐式删除的'Foo'复制构造函数 这很明显 问题1:为什么编译器需要的copy-ctor? ?我期望
..
我喜欢const成员变量的概念,尤其是当我将C函数包装到类中时。构造函数采用资源句柄(例如文件描述符),该句柄在整个对象生命周期内保持有效,而析构函数最后将其关闭。 (那是RAII背后的想法,对吗?) 但是使用C ++ 0x move构造函数时,我遇到了问题。由于析构函数也在“卸载”对象上调用,因此我需要防止清理资源句柄。由于成员变量是const,因此我无法分配值-1或INVALID_HAN
..
我是C ++ 0x的新手,我想把我的头放在右值引用周围,并移动构造函数。我正在使用带有-std = c ++ 0x的g ++ 4.4.6,并且对以下代码感到困惑: class Foo { public: Foo() :p(new int(0)) { printf(“ default ctor\n”); } Foo(int i) :p(new int(i))
..
我试图理解在C ++ 11中移动构造函数和赋值操作的方式,但是在将其委托给父类时遇到了问题。 代码: class T0 { public: T0(){puts(“ ctor 0“); } 〜T0(){puts(“ dtor 0”); } T0(T0 const&){puts(“ copy 0”); } T0(T0&){puts(“ move 0”); } T0&
..
我想使用并使用关键字继承基类的副本构造函数: #include 结构A { A()=默认值; A(const A&){std :: cerr
..
我试图为没有复制构造函数的类实现move构造函数。我收到一个错误,提示缺少该类成员的默认构造函数。 这里有一个简单的例子来说明这一点: 结构A { public: A()=删除; A(A const&)=删除; A(A& a){} }; 结构B { A a; B()=删除; B(B const&)=删除; B(B& b){} };
..
没有移动构造函数但具有接受 const T& 参数的副本构造函数的类型,请满足 std :: is_move_constructible 。例如,在以下代码中: #include struct T { T(const T&){} // T(T&&)= delete; }; int main(){ static_assert(std ::
..
我对何时调用move构造函数与copy构造函数感到困惑。 我已经阅读以下来源: 在C ++ 11中移动语义和右值引用 msdn 所有这些来源要么过于复杂(我只想举一个简单的例子),要么仅显示如何编写move构造函数,而不是如何调用它。我已经写了一个更具体的简单问题: const class noConstruct {} NoConstruct; class a
..
什么功能使我可以将auto用于C ++ 17中的不可复制(和不可移动)类型,而不能用于C ++ 14? 考虑以下代码: struct A{ A(A const&)=delete; A(A&&)=delete; }; int main(){ auto a1 = A{}; // ok in C++17, not ok in C++14 auto&& a
..
只是来自 is_assignable和std :: unique_ptr . @Angew告诉我,因为std::unique_ptr和std::unique_ptr是不同的类型,所以static_assert(not std::is_assignable, std::unique_ptr
..