P1008 ("Prohibit aggregates with user-declared constructors") has become part of the C++20 standard, in order to prevent surprising behavior when using aggregate initialization:
struct X {
int i{42};
X() = delete;
};
int main() {
X x2{3}; // Compiles in C++17, error in C++20
}
I agree that the above X x2{3};
statement should not compile. However, all the examples justifying P1008 that I've encountered are not realistic at all - they are purely syntactical and basically meaningless foo
/bar
/baz
code snippets.
What problem does P1008 solve in practice? I find it hard to imagine how I would end up writing something like the above X
in a real program.
Deleting the default constructor in a C++17 aggregate without providing other constructors to initialize it seems unrealistic to me.
=default
private constructor and all public data members? – Straleyprivate
access classes. If you're given access to an existing value, you can still work on it as normal. – Slopwork