The following code
class A {
public:
A() {} // default constructor
A(int i) {} // second constructor
};
int main() {
A obj({});
}
calls the second constructor. Probably the empty initializer_list
is treated as one argument and is converted to int
. But when you remove the second constructor from the class, it calls the default constructor. Why?
Also, I understand why A obj { {} }
will always call a constructor with one argument as there we are passing one argument which is an empty initializer_list
.
A obj ( {} );
is equivalent toA obj ( A() );
, move-constructingobj
from a default-constructed temporary. – AlcoholizeA(int{})
, whereint{}
is just an elaborate way to write0
. – Alcoholize