C++:如果不对 create/initialize、复制、分配的这些不同方式应用优化,输出是什么?
C++: What is the output, if no optimizations are applied to these different ways to create/initialize, copy, assign?
我发现构建、复制、赋值变量的方式有点混乱,因为在我试过的编译器中,它们通常会应用某种优化(删除临时变量等)。
我在下面的评论中列出了我尝试过的不同方法和我的程序的输出。可能其中一些包含临时对象创建但被编译器优化掉了吗?请说明输出是否符合标准,如果不应用优化,输出是什么。
#include <iostream>
using namespace std;
class type {
public:
type(int z){cout << "ctor"<<endl;};
type(const type&){cout<<"copy"<<endl;}
void operator=(const type& ){cout <<"assign"<<endl;}
};
int main()
{
//constructor
type c(8); //ctor
type c2{8}; //ctor
type c3 = 8; //ctor
type c4 = {8}; //ctor
type c5 = type(8); //ctor
type c6 = type{8}; //ctor
cout <<endl;
//copy
type ci0(c); //copy
type ci1{c}; //copy
type ci2 = c; //copy
type ci3 = {c}; //copy
type ci4 = type(c); //copy
type ci5 = type{c}; //copy
cout <<endl;
//assign
c2 = c; //assign
c2 = {c}; //assign
c2 = type{c}; //copy and then assign
c2 = type(c); //copy and then assign
c2 = 8; //ctor and then assign
c2 = {8}; //ctor and then assign
c2 = type(8); //ctor and then assign
c2 = type{8}; //ctor and then assign
}
使用显式构造函数和复制构造函数并删除每个函数,我能够得到以下结果。
//constructor
type c(8); //explicit ctor
type c2{8}; //explicit ctor
type c3 = 8; //implicit ctor, explicit copy
type c4 = {8}; //implicit ctor
type c5 = type(8); //explicit ctor, implicit copy
type c6 = type{8}; //explicit ctor, implicit copy
cout <<endl;
//copy
type ci0(c); //explicit copy
type ci1{c}; //explicit copy
type ci2 = c; //implicit copy
type ci3 = {c}; //implicit copy
type ci4 = type(c); //implicit copy
type ci5 = type{c}; //implicit copy
cout <<endl;
//assign
c2 = c; //assign
c2 = {c}; //assign
c2 = type{c}; //implicit copy and then assign
c2 = type(c); //implicit copy and then assign
c2 = 8; //implicit ctor and then assign
c2 = {8}; //implicit ctor and then assign
c2 = type(8); //explicit ctor and then assign
c2 = type{8}; //explicit ctor and then assign
我发现构建、复制、赋值变量的方式有点混乱,因为在我试过的编译器中,它们通常会应用某种优化(删除临时变量等)。
我在下面的评论中列出了我尝试过的不同方法和我的程序的输出。可能其中一些包含临时对象创建但被编译器优化掉了吗?请说明输出是否符合标准,如果不应用优化,输出是什么。
#include <iostream>
using namespace std;
class type {
public:
type(int z){cout << "ctor"<<endl;};
type(const type&){cout<<"copy"<<endl;}
void operator=(const type& ){cout <<"assign"<<endl;}
};
int main()
{
//constructor
type c(8); //ctor
type c2{8}; //ctor
type c3 = 8; //ctor
type c4 = {8}; //ctor
type c5 = type(8); //ctor
type c6 = type{8}; //ctor
cout <<endl;
//copy
type ci0(c); //copy
type ci1{c}; //copy
type ci2 = c; //copy
type ci3 = {c}; //copy
type ci4 = type(c); //copy
type ci5 = type{c}; //copy
cout <<endl;
//assign
c2 = c; //assign
c2 = {c}; //assign
c2 = type{c}; //copy and then assign
c2 = type(c); //copy and then assign
c2 = 8; //ctor and then assign
c2 = {8}; //ctor and then assign
c2 = type(8); //ctor and then assign
c2 = type{8}; //ctor and then assign
}
使用显式构造函数和复制构造函数并删除每个函数,我能够得到以下结果。
//constructor
type c(8); //explicit ctor
type c2{8}; //explicit ctor
type c3 = 8; //implicit ctor, explicit copy
type c4 = {8}; //implicit ctor
type c5 = type(8); //explicit ctor, implicit copy
type c6 = type{8}; //explicit ctor, implicit copy
cout <<endl;
//copy
type ci0(c); //explicit copy
type ci1{c}; //explicit copy
type ci2 = c; //implicit copy
type ci3 = {c}; //implicit copy
type ci4 = type(c); //implicit copy
type ci5 = type{c}; //implicit copy
cout <<endl;
//assign
c2 = c; //assign
c2 = {c}; //assign
c2 = type{c}; //implicit copy and then assign
c2 = type(c); //implicit copy and then assign
c2 = 8; //implicit ctor and then assign
c2 = {8}; //implicit ctor and then assign
c2 = type(8); //explicit ctor and then assign
c2 = type{8}; //explicit ctor and then assign