Trying to challenge my knowledge in C++, I created a simple example showing that list initialization provide the same code as using the constructor object:
struct ee{
unsigned m, h , k;
};
class R{
unsigned a,b,c;
ee kl;
public:
R(unsigned p, unsigned q, unsigned r, ee s){ // s is not a reference
a = p;
b = q;
c = r;
kl = s;
}
};
class S{
unsigned a,b,c;
ee kl;
public:
S(unsigned p, unsigned q, unsigned r, ee s): a(p),b(q),c(r), kl(s){}
};
S square(unsigned num, unsigned yum, unsigned po, ee &no) {
if (num > 5){
S a(num + 8, yum - 2, po,no);
return a;
}
else{
S a(num + 18, yum + 3, po + 1, no);
return a;
}
}
R square2(unsigned num, unsigned yum, unsigned po, ee &no) {
if (num > 5){
R a(num + 9, yum + 90, po, no);
return a;
}
else{
R a(num + 34, yum + 12, po + 1, no);
return a;
}
}
You can try it out by yourself here: godbolt
Is there a case where it is still -objectively, in regard to performance- valuable or can we now say that any notation outputs the same code, with the latest compilers?
Update:
Ok this question has not been liked very much and that is fine. The context I am working in, both initialization methods will yield the same results, always, because we cannot use const
nor having a single reference inside the class, nor new/delete or anything else. We pass objects by reference to the methods. That is our guideline guys. And it works. Anyway, thanks you ;)