标签: move-constructor

为什么调用复制构造函数而不是移动构造函数?

我有以下代码:

#include <bits/stdc++.h>

using namespace std;

class A {
public:
    A(const A& a) noexcept { cout << "copy constructor" << endl; }
    A& operator=(const A& a) noexcept { cout << "copy assignment operator" << endl; }
    A(A&& a) noexcept { cout << "move constructor" << endl; }
    A& operator=(A&& a) noexcept { cout << "move assignment operator" << endl; }
    A() { cout << "default constructor" << endl; }
};

vector<A> aList;

void AddData(const A&& a)
{
    aList.push_back(std::move(a));
}

int …
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor move-constructor move-semantics c++11

1
推荐指数
1
解决办法
149
查看次数

如何使用删除默认构造函数的移动构造函数

我有一段这样的现有代码:

struct Base {
    Base() = default;
};

struct Derive: public Base
{
    Derive() = default;
    Derive(const Derive&) = delete;
    Derive(Derive&& p) { *this = std::move(p); }
    Derive& operator = (const Derive& p) = delete;
    Derive& operator = (Derive&& p) {
        return *this;
    }
};

int main() {
  Derive p;
}
Run Code Online (Sandbox Code Playgroud)

它编译并运行。现在我想稍微更改类定义,以便 Base 或 Derived 类始终使用某些整数参数构造,并且永远不会在没有此类参数的情况下构造。

因此,如果我尝试以下更改:

struct Base {
    Base() = delete;
    Base(int a_) : a{a_} {};
private:
  int a; //new mandatory param;
};

struct Derive: public Base
{ …
Run Code Online (Sandbox Code Playgroud)

c++ class move-constructor

1
推荐指数
1
解决办法
55
查看次数

我可以在没有赋值运算符的类型的向量中可靠地 emplace_back 吗?

我在 GCC、Clang 和 MSVC 中做了一些测试,发现emplace_back永远不会在包含的类上调用赋值运算符。它仅在重新分配时调用复制或移动构造函数。标准是否以某种方式保证这种行为?

用例是我有一些类按顺序存储在一个数字中,该数字只会随着时间的推移而增长,直到整个向量被破坏。我很乐意从赋值运算符中清除我的代码。

c++ allocation vector move-constructor move-assignment-operator

1
推荐指数
1
解决办法
91
查看次数

c++何时向量push_back深复制对象?

我创建了一个向量并使用push_back将几个节点对象放入其中。但是,我无法预测何时将使用移动构造函数或复制构造函数。

当push_back使用复制构造函数或移动构造函数时有什么模式吗?c++参考说它有时会复制,有时会移动,但从未详细说明它们何时做什么。

#include <iostream>
#include <vector>
#include <unordered_set>
#include <set>
#include <unordered_map>
#include <map>
#include <queue>
using namespace std;
struct Node {
    int val;
    
    Node(int val) : val(val) {
        cout<<"created object" << val<<endl;
    }
    
    Node(const Node& m) : val(m.val) {
        cout<<"copy constructor is called on value " << m.val << endl;
    }
    
    ~Node() {
        cout<<"destroyed val" << val<<endl;
    }


    Node(Node&& other) noexcept 
        : val(other.val) {
            cout<<"moved val " << other.val << endl;
    }


};


void f(vector<Node>& a) {
    cout<<"______________________"<<endl;
    Node …
Run Code Online (Sandbox Code Playgroud)

c++ destructor copy-constructor move-constructor

1
推荐指数
1
解决办法
137
查看次数

C++ 0x移动构造函数

编辑:我在这里重新问了同样的问题(解决了这个问题后面的问题):为什么这个C++ 0x程序会产生意外的输出?

基本的想法是,如果你不小心,指向可移动的东西可能会给你带来一些奇怪的结果.


C++移动构造函数和移动赋值运算符看起来非常积极.它们可以在复制构造函数没有意义的情况下使用,因为它们不需要重复指向的资源.

但有些情况下,如果你不小心,他们会咬你.这是特别相关的,因为我已经看到允许编译器生成移动构造函数的默认实现的建议.如果有人能给我一个,我会提供一个链接.

所以,这里有一些代码有一些可能不完全明显的缺陷.我测试了代码,以确保它使用-std=gnuc++0x标志以g ++编译.这些缺陷是什么?你会如何修复它们?

#if (__cplusplus <= 199711L) && !defined(__GXX_EXPERIMENTAL_CXX0X__)
   #error This requires c++0x
#endif

#include <unordered_set>
#include <vector>
#include <utility>
#include <algorithm>

class ObserverInterface {
 public:
   virtual ~ObserverInterface() {}

   virtual void observedChanged() = 0;
   virtual void observedGoingAway() = 0;
};

class Observed {
 private:
   typedef ::std::unordered_set<ObserverInterface *> obcontainer_t;

 public:
   Observed() {}
   Observed(const Observed &) = delete;
   const Observed &operator =(const Observed &b) = delete;
   // g++ does not currently support defaulting the …
Run Code Online (Sandbox Code Playgroud)

c++ move-constructor c++11

0
推荐指数
1
解决办法
5743
查看次数

既不复制也不移动构造函数

可能重复:
为什么在这种情况下不调用复制构造函数?
什么是复制省略和返回值优化?

任何人都可以向我解释为什么以下程序产生输出"cpy:0"(至少在使用g ++ 4.5.2编译时):

#include<iostream>

struct A {

    bool cpy;

    A() : cpy (false) {
    }

    A (const A & a) : cpy (true) {
    }

    A (A && a) : cpy (true) {
    };

};

A returnA () { return A (); }

int main() {

    A a ( returnA () );
    std::cerr << "cpy: " << a.cpy << "\n";
}
Run Code Online (Sandbox Code Playgroud)

当我试图弄清楚这个例子看似奇怪的结果时出现了问题:使用常量数据成员或引用成员移动类的ctor

c++ copy-constructor move-constructor

0
推荐指数
1
解决办法
185
查看次数

用memcpy移动构造函数

我有一个结构,我想要不可复制,只能移动,但因为它包含大量的POD,写移动构造函数会很长,忘记变量将很难调试.例:

struct myStruct{
    int a,b,c,d;
    double e,f,g,h;
    std::complex<double> value1,value2;

    std::unique_ptr<Calculator> calc;

    myStruct(){}
    myStruct(const myStruct &)=delete;
    myStruct(myStruct && other);
};
Run Code Online (Sandbox Code Playgroud)

这种移动构造函数会出现什么问题:

myStruct::myStruct(myStruct && other){
    std::memcpy(this,&other,sizeof(myStruct));
    other.calc.release();
    calc->rebind(this);
}
Run Code Online (Sandbox Code Playgroud)

我可以面对什么问题,这是明确定义的吗?

c++ move-constructor c++11

0
推荐指数
1
解决办法
1100
查看次数

C++中是否有部分拷贝构造函数?

有没有办法在C++中实现复制构造函数,它只复制某些特定成员并为其他成员实现移动.

例如,我有一个班级

class partialCopy
{
   int a;
   int largeArray[1000] ;
}
Run Code Online (Sandbox Code Playgroud)

现在假设使用移动构造函数我想只保留largeArray两个对象之间的一个副本并使用副本我可以在同一个两个对象之间保留整数a的单独副本.

编码时可能会出现这种情况.

任何人都可以分享这个想法吗?

c++ copy-constructor move-constructor

0
推荐指数
1
解决办法
310
查看次数

继承和删除复制和移动构造函数

我想要一个具有第三级继承的类.我不会移动我的课程或复制它们.实际上,它们只会创建一次.我想测试是否可以删除移动和复制构造函数和赋值,如下所示:

MyClass ( MyClass && ) = delete;
MyClass ( const MyClass & ) = delete;
MyClass & MyClass :: operator= ( const MyClass & ) = delete;
MyClass & MyClass :: operator= ( MyClass && ) = delete;
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我在基类构造函数和基类的基础上得到一个错误:

initializing argument 1 of ‘Base::Base(const int&)’ [-fpermissive]
Run Code Online (Sandbox Code Playgroud)

我的构造函数是这样的:

Base(const string & name):BaseOfBase(name){

    };
Run Code Online (Sandbox Code Playgroud)

我的问题是:

  1. 是否可以删除所有移动和复制构造函数和赋值运算符?
  2. 如果有可能,为什么我会收到错误?
  3. 如果可能的话,这是一个不错的选择吗?

c++ copy-constructor move-constructor c++11

0
推荐指数
1
解决办法
332
查看次数

在C++ 11中移动语义

我想完全理解C++ 11中的移动语义.所以我编写了几个类来查看何时调用不同的构造函数:

#include <iostream>
using namespace std;

class A {
public:
    A() : a1_(0) {std::cout << "Calling constructor" << std::endl;}
    A(A&& other) {
        std::cout << "Calling move constructor" << std::endl;
        a1_ = other.a1_;
        other.a1_ = 0;
    }

    // Move assignment operator.
    A& operator=(A&& other) {
        std::cout << "Calling move operator" << std::endl;
        if (this != &other) {
            a1_ = other.a1_;
            other.a1_ = 0;
        }
        return *this;
    }

    // Copy constructor.
    A(const A& other) {
        std::cout << "Calling copy constructor" << …
Run Code Online (Sandbox Code Playgroud)

c++ operators move-constructor move-semantics c++11

0
推荐指数
1
解决办法
134
查看次数

C++ 三元运算符调用复制构造函数而不是移动构造函数

我正在使用 VS Code 在 Linux 中使用 g++ 进行编译和调试。

需要包括和使用:

#include <string>
#include <iostream>

using namespace std;
Run Code Online (Sandbox Code Playgroud)

这是我的类是可移动的:

class A {
public:
    A(const string& strA) : strA(strA) {}

    A(const A& a) : A(a.strA) {
    }

    A(A&& a) : A(a.strA) {
        a.strA = "";
    }

    string strA;
};
Run Code Online (Sandbox Code Playgroud)

返回 A 实例的示例函数:

A RetA() {
    A a("a");
    A b("bha");

    string ex;
    cin >> ex;
    a.strA += ex;

    return ex == "123" ? a : b;
}
Run Code Online (Sandbox Code Playgroud)

这是简单的主要内容:

int main() {
    A a(RetA());

    return …
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor move-constructor

0
推荐指数
1
解决办法
67
查看次数

移动构造函数/运算符=

我正在尝试学习C++的新功能,即移动构造函数和赋值X::operator=(X&&),我发现了一些有趣的例子, 但我唯一不理解但更不同意的是移动ctor和赋值运算符中的一行(在下面的代码中标记):

MemoryBlock(MemoryBlock&& other)
   : _data(NULL)
   , _length(0)
{
   std::cout << "In MemoryBlock(MemoryBlock&&). length = " 
             << other._length << ". Moving resource." << std::endl;

   // Copy the data pointer and its length from the 
   // source object.
   _data = other._data;
   _length = other._length;

   // Release the data pointer from the source object so that
   // the destructor does not free the memory multiple times.
   other._data = NULL;
   other._length = 0;//WHY WOULD I EVEN BOTHER TO SET IT …
Run Code Online (Sandbox Code Playgroud)

c++ constructor move-constructor move-semantics c++11

-1
推荐指数
1
解决办法
1497
查看次数

如何完美转发一个常量引用或可移动右值的通用引用?

我用 C++20 编写了一个无锁且线程安全的环形队列,到目前为止它可以工作。唯一不完美的是它必须有两个enque()方法,一个接受对左值的 const 引用作为参数,另一个接受对右值的引用,以便将右值移入队列而不是再次构造。

\n

之前版本的代码如下,只是一个骨架,进行简化:

\n
template <typename T>\nclass RingQue_t {\npublic:\n    explicit RingQue_t( size_t capacity );\n    ~RingQue_t();\n    bool deque( T& dest_ ) { return true; };\n\n    // If we offer a const ref, the compiler will select this one\n    bool enque( const T& src_ ) {\n        // a lot of same codes goes here for a same logic...\n\n        new( _buff + _tail ) T( src_ );\n    };\n\n    // If we offer a rvalue ref, the compiler …
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor rvalue-reference move-constructor perfect-forwarding

-1
推荐指数
1
解决办法
160
查看次数