Consider the following code
#include <iostream>
class my_class {
public:
static my_class& factory(int val) {
static my_class instance{val};
return instance;
}
private:
my_class(int value) {
std::cout<<value<<std::endl;
};
};
int main() {
auto& i77 = my_class::factory(77); // call #1
auto& i1 = my_class::factory(1); // call #2
return 0;
}
The output is 77. On the call #2 constructor isn't called.
I understand what happens in call #1. Compiler allocates memory for instance at compile time and calls constructor on enetering my_class::factory(77);
But what happens on call #2? Why constructor isn't called? Is this behavior standard?