I have browsed SO regarding initializer lists and member initialization and haven't found an answer to this specific issue. Please excuse me if it does turn out to be a duplicate.
I'm browsing C++ source code where a member FILE*
of a struct is initialized like so:
struct Something {
FILE* d_fp{0};
}
I have Googled it, and I understand the concept of initialization using an initializer list constructor.
I understand that if no dedicated initializer-list constructor is present on the instantiated type, a different constructor with a matching number of arguments is called. So saying MyType o{0}
is the same as MyType o(0)
. Please correct me if my understanding is false.
However, in this case, changing the code to read FILE* d_fp(0)
results in compiler errors:
main.cpp:47:11: error: expected ';' at end of member declaration
47 | FILE* d_fp(0);
| ^~~~
| ;
main.cpp:47:16: error: expected unqualified-id before numeric constant
47 | FILE* d_fp(0);
| ^
main.cpp:47:16: error: expected ')' before numeric constant
47 | FILE* d_fp(0);
| ~^
| )
So two questions follow:
Why does
d_fp(0)
not do the same thing asd_fp{0}
in this case?What might be the reason a programmer chooses
d_fp{0}
initialization instead of the more intuitived_fp = 0
? Is it simply a matter of taste, or is it not actually the same thing?