What's the difference between ()
and {}
when constructing objects?
I think {} should only support with initializer_list
or an array, but when I run below snip, I confused.
#include <iostream>
using namespace std;
struct S {
int v=0;
S(int l) : v(l) {
}
};
int main()
{
S s1(12); // statement1
S s2{12}; // statement2
cout << s1.v << endl;
cout << s2.v << endl;
}
statement1
is right because ()
is the basic grammar for constructing the object.
I expect the statement2
will be compiled failed. I think {}
is only can be used for an array or initializer_list
type. but the actual result is compiled perfectly without error.
what do I miss?
Aucun commentaire:
Enregistrer un commentaire