简体   繁体   English

为什么从未调用我的复制赋值运算符?

[英]Why is my copy assignment operator never called?

I' playing around with c++14 a bit and I am wondering why my assignment operator is never called. 我正在玩c ++ 14,我想知道为什么我的赋值运算符从未被调用过。 The implementation appears to be correct and I disabled the optimisations ( -fno-elide-constructors -O0 ) Is this some kind of compiler optimisation I am missing or is something wrong with my code? 实现似乎是正确的,我禁用了优化( -fno-elide-constructors -O0 )这是我缺少的某种编译器优化还是我的代码有问题?

Source Code 源代码

#include <iostream>

using namespace std;

int num = 0;

#define LOG_LINE(a) cout << "\n" << (++num) << ".)------------------------> " << #a << "\n"
#define LOG_TEXT cout << "called " << __PRETTY_FUNCTION__ << "\n"

struct Klass {
    Klass() { LOG_TEXT; }
    ~Klass() { LOG_TEXT; }

    // copy
    Klass(const Klass&) { LOG_TEXT; }
    Klass& operator=(const Klass&) { LOG_TEXT; return *this; }

    // move
    Klass(Klass&&) { LOG_TEXT; }
    Klass& operator=(Klass&&) { LOG_TEXT; return *this; }
};


int main() {

    LOG_LINE(expecting normal contruction - OK);
    auto k1 = Klass{};
    auto k12 = Klass();

    LOG_LINE(expecting assignment operator - FAIL);
    auto k2 = k1;

    LOG_LINE(expecting copy construction - OK);
    auto k3 = Klass(k2);

    LOG_LINE(expecting move assignment - FAIL);
    auto k4 = std::move(k3);

    LOG_LINE(expecting move construction - OK);
    auto k5 = Klass(std::move(k4));

    LOG_LINE(expecting destruction of remaining objects - OK);
    (void) k5;

    return 0;
}

Output 产量

1.)------------------------> expecting normal contruction - OK
called Klass::Klass()
called Klass::Klass(Klass &&)
called Klass::~Klass()
called Klass::Klass()
called Klass::Klass(Klass &&)
called Klass::~Klass()

2.)------------------------> expecting assignment operator - FAIL
called Klass::Klass(const Klass &)

3.)------------------------> expecting copy construction - OK
called Klass::Klass(const Klass &)
called Klass::Klass(Klass &&)
called Klass::~Klass()

4.)------------------------> expecting move assignment - FAIL
called Klass::Klass(Klass &&)

5.)------------------------> expecting move construction - OK
called Klass::Klass(Klass &&)
called Klass::Klass(Klass &&)
called Klass::~Klass()

6.)------------------------> expecting destruction of remaining objects - OK
called Klass::~Klass()
called Klass::~Klass()
called Klass::~Klass()
called Klass::~Klass()
called Klass::~Klass()
called Klass::~Klass()

Additional Information 附加信息

I am using: 我在用:

clang ++ --version
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix

compiled with: clang++ source.cpp -std=c++1y -os -Wpedantic -Wall -Wextra -fno-elide-constructors -O0 && ./s 编译: clang++ source.cpp -std=c++1y -os -Wpedantic -Wall -Wextra -fno-elide-constructors -O0 && ./s

auto k2 = k1;

is copy initialization, not assignment; 是复制初始化,而不是赋值; it'll call the copy constructor. 它会调用复制构造函数。 If you change that line to the following it'll do what you expect 如果您将该行更改为以下内容,它将按预期执行

Klass k2;   // default construction
k2 = k1;    // copy assignment

Similarly 同样

Klass k4;            // default construction
k4 = std::move(k3);  // move assignment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不调用复制赋值运算符 = - Copy assignment operator= is not called 何时调用复制赋值运算符? - When is the copy assignment operator called? 为什么在这种情况下都调用复制构造函数和赋值运算符 - why copy constructor & assignment operator both are called in this case 为什么在调用重载的赋值运算符时调用了拷贝构造函数? - why copy constructor is called at the time of calling overloaded assignment operator? 为什么在赋值运算符之后在此代码中调用复制构造函数? - Why is the copy constructor called in this code after the assignment operator? 为什么在这种情况下不调用赋值运算符而支持复制构造函数? - Why is the assignment operator not called in this case in favor of the copy constructor? C ++移动语义:为什么调用复制赋值operator =(&)而不是移动赋值operator =(&amp;&amp;)? - C++ move semantics: why copy assignment operator=(&) is called instead of move assignment operator=(&&)? 为什么要为单个赋值操作调用复制构造函数和重载赋值运算符? - Why copy constructor and overloaded assignment operator are being called for a single assignment operation? 复制构造函数和赋值运算符都被调用 - Copy constructor and assignment operator both get called 为什么赋值运算符同时调用复制ctor和赋值运算符? - Why does assignment operator call both copy ctor and assignment operator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM