0

If I have a class with two constructors like this:

class Test {

    public:

        Test(int x) {
            _num = x;
        }

        Test() {
            _num = 0;
        }

    private:

        int _num;
};

I want to create a stack object based on a condition like this:

    Test test;
    if (someCondition() == 23) {
        test = Test(42);
    }

Will I have the overhead of creating a Test object two times, calling both constructors, in this case? Or will this be optimized out in general? Is this considered good practice?

Toy-examples in compiler explorer are optimized heavily with inlining with no apparent constructor call left. So it's not really clear to me.

aurel
  • 1
  • 1
  • 2
  • Build with optimizations enabled, and look at the generated machine code. – Some programmer dude Dec 21 '22 at 09:17
  • 1
    I would expect the code generated from this to be as efficient as if you had declared `test` as an `int` variable. But the only way to know is to turn on optimization and examine the generated machine code. – john Dec 21 '22 at 09:17
  • Might depend how complex your real code is. There's no guarantee that the compiler will optimize it out though. Only way to know for sure is to look at the disassembly of your code. – ChrisMM Dec 21 '22 at 09:17
  • 4
    On another note, while I know that this might be very simplified code, you don't really need two constructors here. Use a default argument for the first one and it can be used as a default constructor. I also recommend you learn about *constructor initializer lists*, to properly *initialize* your members rather than just assigning to the member variables. – Some programmer dude Dec 21 '22 at 09:17
  • 2
    Looks like attempt micro-optimization (or premature optimization). At your current skill level there is no point on wondering if this is optimal or not. Compilers are really really smart and there is "as-if rule" which allows them to do lots of smart things. This makes code tweaks you are thinking of completely useless. – Marek R Dec 21 '22 at 09:25
  • 1
    I wouldn't rely on the compiler optimising if there's a **simple** solution not needing to do so, e.g. `auto test = someCondition() == 23 : Test(42) : Test()`; – Aconcagua Dec 21 '22 at 09:26
  • Does this answer your question? https://stackoverflow.com/questions/15718262/what-exactly-is-the-as-if-rule – 463035818_is_not_an_ai Dec 21 '22 at 10:07

1 Answers1

0

Write code to express intent.

You do not want to call the constuctor twice. Don't call the constructor twice:

Test test = (someCondition() == 23) ? Test() : Test(42);

Can the compiler optimize your code to have only one Test constructed?

Yes. Compiler optimizations must follow the so-called "as-if rule". In a nutshell: Creating two, one, or none, or 100 instances in your code has no observable effect. The compiler can notice that and produce a program where no instance is created.

Will the compiler optimize your code to have only one Test constructed?

You cannot tell unless you try (or you are a compiler writer and your brain is capable of doing the job of a compiler... then you can know without trying). That's why it is important to write code to express intent. The code you write is not instructions for the CPU, but instructions for the compiler to generate instructions for the CPU.

bcsb1001
  • 2,834
  • 3
  • 24
  • 35
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185