提问人:pasha 提问时间:8/13/2021 最后编辑:pasha 更新时间:8/16/2021 访问量:106
C++:如果没有对这些不同的创建/初始化、复制、分配方式进行优化,输出是什么?
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
}
答:
0赞
pasha
8/16/2021
#1
使用显式 to ctor 和复制 ctor 并删除每个函数,我能够得到以下结果。
//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
上一个:使用向量时 [] 运算符的问题
评论
type ci4 = {c}
type ci5 = type{c}
type ci6 = type(c)
copy