-6

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 constnor 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 ;)

Community
  • 1
  • 1
Kroma
  • 1,109
  • 9
  • 18
  • 3
    You need it not only for performance reasons in some cases you cannot assign to members. And how about base class initialization? – Slava Jun 16 '19 at 15:16
  • You are not enforced to use inheritance. Can you develop the cases you can't assign to members? – Kroma Jun 16 '19 at 15:17
  • No, it is technically not the same code. `S` calls the constructor, while `R` calls the assignment operator. The effect should be similar, but **technically** they are not the same thing. –  Jun 16 '19 at 15:18
  • 4
    @Kroma What if the members are `const`? – HolyBlackCat Jun 16 '19 at 15:19
  • @Kroma sure reference is one example. Any class which assignment is prohibited or at least more expensive than ctor. – Slava Jun 16 '19 at 15:19
  • *"Is there a case where it is still -objectively, in regard to performance- valuable"* http://quick-bench.com/3oqHnv87C6rYI_qUfJFOg7_4x6w – HolyBlackCat Jun 16 '19 at 15:25
  • @HolyBlackCat: same perf with gcc 8.2, but your const argument holds true – Kroma Jun 16 '19 at 15:28
  • Say you have a class defined in a shared library: https://godbolt.org/z/8FQ-pi. It has to call the default constructor first and then `operator=`, as opposed to just the copy constructor. – Artyer Jun 16 '19 at 15:31
  • Add a member to your class that has no default constructor... – Retired Ninja Jun 16 '19 at 15:31
  • @RetiredNinja: real question: why would one do that? – Kroma Jun 16 '19 at 15:32
  • @Artyer: Linked& operator=(const Linked&); is the culprit and should not be there in the first place. – Kroma Jun 16 '19 at 15:34
  • @Kroma Any number of reasons. Just because you cannot think of one doesn't mean it doesn't break your assertion that the initializer list is useless. – Retired Ninja Jun 16 '19 at 15:46
  • @RetiredNinja: no agressivity here, really, but could you please develop? – Kroma Jun 16 '19 at 16:00
  • https://stackoverflow.com/questions/926752/why-should-i-prefer-to-use-member-initialization-list – Retired Ninja Jun 16 '19 at 16:19

1 Answers1

0

Using the initializer list and assigning in the constructor are not the same. Members are initialized anyhow. If you don't do it then they are default initialized. Only initialization is in general cheaper than initialization and then assignment. Maybe in your case the compiler emitted similar or even the same code, but there are sereval things you cannot do with assigning in the constructor:

  • const members can only be initialized, not assigned to
  • reference members must be initialized
  • members with no default constructor cannot be default default initialized, but you need to initialize them
  • maybe more that I missed
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • You are right. Giving a bit more of context to my question in the update though. Thanks! – Kroma Jun 16 '19 at 19:12