No, @frek, you didn't.
There is a very big difference between
T t();
which I alluded to, and
T();
which you have just mentioned.
The first tries (unsuccessfully) to declare a function called t that returns an entity of type T. (It would compile - albeit not do what you presumably intended - if you declared it outside of any other function, including main()). The second (successfully if there is one) invokes a constructor of T with no parameters.
The following will invoke compiler warnings, but not errors. It will produce garbage, obviously, but will run.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
struct T {
float f;
int gn;
char ch;
};
T t(); // <==== Move it here. Note the (). This is the declaration of a function, NOT a variable of type T
int main() {
T t; // <==== Unfortunate use of the same name, but not an error. Note: no (). This is a variable of type T.
std::cout << t.gn << ' ' << t.f << ' ' << int(t.ch); // garbage, obviously, but a separate issue
}
|
frek wrote: |
---|
So I think there are only two ways to assure us about the built-in types to be default initialized:
Initialized them, or use empty curly braces.
If you disagree, please tell me. |
I disagree.
"default initialized" means that they would be initialised
without any action on your part.
If you initialise them by explicitly stating a value then clearly you aren't default-initialising!
If you use empty curly braces then you ... are initialising them with a type equivalent of a zero. "Uniform initialisation" or "brace initialisation" or "initialising to a default value", not "default initialisation". You are assigning a value, even if you don't state that value explicitly.