Reputation: 8401
This code:
class A {
public:
A() = default;
};
int main()
{
const A a;
return 0;
}
can be compiled without errors in Visual C++ (from VS 2015 Update 2) and GCC (5.3.1), but can not be compiled with Clang, it gives this error:
test.cpp:8:13: error: default initialization of an object of const type
'const A' without a user-provided default constructor
const A a;
^
{}
1 error generated.
Who is right, clang or gcc and MSVC? Are other compilers behavior is a bug?
PS. const A a{};
is compiled without errors with all three.
Same if we define empty constructor: A::A() {}
.
PPS. This is NOT a duplicate. I emphasize the actual question.
Update Filed a bug in MSVC: https://connect.microsoft.com/VisualStudio/feedback/details/2538005 S.T.L. said that they are in process of fixing them all to conform clang tests: https://twitter.com/StephanTLavavej/status/715923311796953089
Upvotes: 3
Views: 287
Reputation: 157414
gcc is conforming to DR253; Why does gcc allow a const object without a user-declared default constructor but not clang? Note that gcc does not require the presence of the explicitly-defaulted default constructor:
struct A {}; // No uninitialized members
int main() {
const A a; // OK with gcc
}
MSVC is non-conformant in that it will allow compilation of the code when A
has an uninitialized data member, regardless of whether there is an explicitly-defaulted default constructor, but it does at least issue a diagnostic (C4269) so it should be easy to make conformant:
struct A { int i; }; // Uninitialized data member
int main() {
const A a; // Should be ill-formed; MSVC accepts with diagnostic
}
main.cpp(8): warning C4269: 'a': 'const' automatic data initialized with compiler generated default constructor produces unreliable results
clang has a patch accepted and ready to land (since June 2015) but it appears to have been forgotten.
Upvotes: 2