Using {}
instead of ()
in a constructor will allow me to initialize class members with a specific constructor right in the header, like so:
class X {
private:
std::vector y{1, 2, 3};
};
But how do I know that for a class Z
I use Z z{a, b};
will call the constructor that has two parameters — Z::Z(int a, int b)
— and not the one with the std::initializer_list
?
I mean std::complex(1, 2)
and std::complex{1, 2}
are the same, but std::vector(3)
and std::vector{3}
are certainly not.
Should I always use the {}
variant or use ()
unless I need {}
?