In C++ 20, for a type like this:
struct Person {
std::string name;
int age;
};
We can return it from a function using aggregate initialization, with or without writing out the type name:
Person getJohn()
{
// With type name
return Person {
.name = "John",
.age = 42,
};
// Without type name
return {
.name = "John",
.age = 42,
};
}
What's the difference between specifying and not specifying the type name explicitly? One thing I noticed is that without a type name, if I mess up the field names/order, the error message given by my compiler and IDE is less clear than if I have a type name. But other than that, are the two forms semantically different in any way?