Compile the following program with g++ and clang++ and compare the resulting output from each compiler. Compiler instructions and output follow this program.
Note in the output that g++ seems to have a superior error message.
#include <cstdio>
using namespace std;
class my_object {
public:
int id_;
my_object() { }
my_object(int id) : id_(id)
{
printf( "constructed my_object %d\n", id_ );
}
~my_object()
{
printf( "destructed my_object\n" );
}
int get_id() { return id_; }
};
class X {
public:
private:
union {
char storage [sizeof(my_object)];
my_object object;
} y;
};
int main()
{
X x;
}
g++ command line and output:
// libcxx references can be omitted in your tests.
c:\test>g++ -fno-exceptions -static -std=c++11 -Wall moslow2.cpp -I/libcxx/include -L/libcxx_build/lib -lc++ -omo2.exe
moslow2.cpp: In function 'int main()':
moslow2.cpp:30:7: error: use of deleted function 'X::X()'
X x;
^
moslow2.cpp:18:7: note: 'X::X()' is implicitly deleted because the default definition would be ill-formed:
class X {
^
moslow2.cpp:18:7: error: use of deleted function 'X::<anonymous union>::<constructor>()'
moslow2.cpp:22:11: note: 'X::<anonymous union>::<constructor>()' is implicitly deleted because the default definition woul
d be ill-formed:
union {
^
moslow2.cpp:24:19: error: union member 'X::<anonymous union>::object' with non-trivial 'my_object::my_object()'
my_object object;
^
moslow2.cpp:18:7: error: use of deleted function 'X::<anonymous union>::~<constructor>()'
class X {
^
moslow2.cpp:22:11: note: 'X::<anonymous union>::~<constructor>()' is implicitly deleted because the default definition wou
ld be ill-formed:
union {
^
moslow2.cpp:24:19: error: union member 'X::<anonymous union>::object' with non-trivial 'my_object::~my_object()'
my_object object;
^
moslow2.cpp:30:7: error: use of deleted function 'X::~X()'
X x;
^
moslow2.cpp:18:7: note: 'X::~X()' is implicitly deleted because the default definition would be ill-formed:
class X {
^
moslow2.cpp:18:7: error: use of deleted function 'X::<anonymous union>::~<constructor>()'
c:\test>
Clang compile land and output:
c:\test>clang++ -fno-exceptions -static -std=c++11 -Wall moslow2.cpp -I/libcxx/include -L/libcxx_build/lib -lc++ -omo2.exe
moslow2.cpp:30:7: error: call to implicitly-deleted default constructor of 'X'
X x;
^
moslow2.cpp:25:7: note: default constructor of 'X' is implicitly deleted because field 'y' has a deleted default
constructor
} y;
^
moslow2.cpp:24:19: note: default constructor of '' is implicitly deleted because variant field 'object' has a non-trivial
default constructor
my_object object;
^
1 error generated.
*** I find the g++ error has more clarity then the clang++ error. Note that clang++ seems to use '' to specify anonymous but it looks like a mistake.
Suggestion, remove use if '' and get nearer to the g++ error.
:24:19: error: union member 'object' has a non-trivial constructor
my_object object;
^
:7:5: note: because type 'my_object' has a user-provided default constructor
my_object() { }
^
1 error generated.
Extended Description
Compile the following program with g++ and clang++ and compare the resulting output from each compiler. Compiler instructions and output follow this program. Note in the output that g++ seems to have a superior error message.
g++ command line and output:
// libcxx references can be omitted in your tests.
Clang compile land and output:
*** I find the g++ error has more clarity then the clang++ error. Note that clang++ seems to use '' to specify anonymous but it looks like a mistake.
Suggestion, remove use if '' and get nearer to the g++ error.