假设我有这种假设的,奇怪的和不直观的情况
#include <iostream>
struct A
{
A()
{
member = 1;
}
A(const A &)
{
member = 2;
}
int member;
};
int main()
{
A a = A();
A b = a;
std::cout << a.member << std::endl;
std::cout << b.member << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道复制省略意味着a将使用默认构造函数初始化,并且将使用复制构造函数初始化b.我也知道(至少在gcc上)你可以告诉编译器不要做任何复制省略.
我的问题是有一些方法让编译器不为这个类使用copy elision 吗?
我意识到在任何实际情况下的答案都是99.9%的时间找到其他方式,我没有0.01%的案例中的一个(这是一个实际的假设问题,而不是"假设性问题")
以下代码不会调用复制构造函数.
struct X
{
int x;
X(int num)
{
x = num;
std::cout << "ctor" << std::endl;
}
X(const X& other)
{
std::cout << "copy ctor" << std::endl;
}
};
int main(int argc, _TCHAR* argv[])
{
X* x = new X(3);
X* y(x);
}
Run Code Online (Sandbox Code Playgroud)
输出:
ctor
Run Code Online (Sandbox Code Playgroud)
是复制品吗?
下面哪两个应该是首选,为什么?
struct X {
Y data_;
explicit X(Y&& data): data_(std::forward<Y>(data)) {}
};
Run Code Online (Sandbox Code Playgroud)
VS
struct X {
Y data_;
explicit X(Y data): data_(std::move(data)) {}
};
Run Code Online (Sandbox Code Playgroud) 是否允许C++编译器替换:
const auto myType = MyType(1, 2, 3);
Run Code Online (Sandbox Code Playgroud)
有:
const MyType myType(1, 2, 3);
Run Code Online (Sandbox Code Playgroud)
即,发出任务,或有什么可以防止这种情况?
注意:我问的原因是我更喜欢第一个版本.
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << "A()" << endl;
}
A(const A& a) {
cout << "A(const A& a)" << endl;
}
A(A&& a) {
cout << "A(A&& a)" << endl;
}
A& operator=(const A& a) {
cout << "operator=(const A& a)" << endl;
return *this;
}
A& operator=(A&& a) {
cout << "operator=(A&& a)" << endl;
return *this;
}
~A() {
cout << "~A()" << endl;
};
};
A foo() {
A …Run Code Online (Sandbox Code Playgroud) template <typename Widget>
Widget frobnicate(Widget w) {
// optionally mutate w in some way
return w;
}
Run Code Online (Sandbox Code Playgroud)
如果Widget实现移动构造函数,答案是否会改变?
我应该搬家回来吗?例如,在这种情况下:
return std::move(w);
Run Code Online (Sandbox Code Playgroud) 我试图让复制省略来处理要返回的对象的字段。
示例代码:
#include <iostream>
struct A {
bool x;
A(bool x) : x(x) {
std::cout << "A constructed" << std::endl;
}
A(const A &other) : x(other.x) {
std::cout << "A copied" << std::endl;
}
A(A &&other) : x(other.x) {
std::cout << "A moved" << std::endl;
}
A &operator=(const A &other) {
std::cout << "A reassigned" << std::endl;
if (this != &other) {
x = other.x;
}
return *this;
}
};
struct B {
A a;
B(const A &a) : a(a) …Run Code Online (Sandbox Code Playgroud) 我试图理解使用l/r值调用的构造函数,所以我创建了class A以下内容:
class A {
public :
A() { cout << "called default constructor" << endl ; }
A(const A&) { cout << "called copy constructor" << endl ; }
A(const A&&) { cout << "called move constructor" << endl ; }
};
Run Code Online (Sandbox Code Playgroud)
在主函数中,我创建了一个实例 a
int main()
{
A a(A());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是在已实现的构造函数中没有调用构造函数!
有什么解释吗?谢谢你!
我有巨大的结构 DataFrom 和 Data (实际上有不同的成员)。数据是从 DataFrom 创建的。
struct DataFrom{
int a = 1;
int b = 2;
};
static DataFrom dataFrom;
struct Data{
int a;
int b;
};
class DataHandler{
public:
static Data getData(const DataFrom& data2){
Data data;
setA(data, data2);
setB(data, data2);
return data;
}
private:
static void setA(Data& dest, const DataFrom& source){
dest.a = source.a;
}
static void setB(Data& dest, const DataFrom& source){
dest.b = source.b;
}
};
int main(){
auto data = DataHandler2::getData(dataFrom); // copy of whole Data …Run Code Online (Sandbox Code Playgroud) C++ 标准允许(要求)编译器优化对复制构造函数的调用(在某些情况下)的原因是什么,即使它可能包含可观察到的副作用?
如果我没记错的话,“好像”规则已经允许编译器优化掉不必要的代码,只要生成的程序模拟标准中定义的抽象机器的可观察行为。
制定例外规则的动机是什么?它不会在语言中造成不一致吗?方便(或必要)吗?
c++ design-rationale compiler-optimization copy-elision as-if
c++ ×10
copy-elision ×10
constructor ×4
c++17 ×3
c++11 ×2
as-if ×1
class ×1
destructor ×1
in-place ×1
optimization ×1