C2578 initializing anonymous unions with const pointer fields

G

Guest

Is this a bug with the ms compiler (V7.1)? (It seems so at least.)

I get errors when I don't initialize all the const pointer fields of an
anonymous union in a struct. Example:

//T2.h
typedef int Type1;
typedef float Type2;
struct S1
{
union
{
const Type1 * const m_pType1;
const Type2 * const m_pType2;
};

explicit S1(const Type1 * const pType1);
~S1();
};

//T2.cpp
S1::S1(const Type1 * const pType1) : m_pType1(pType1)
//, m_pType2((const Type2 * const)pType1)
{
}

S1::~S1()
{
}

The above gives the following error:

error C2758: 'S1::$UnnamedClass$0x512ae669$11$::m_pType2' : must be
initialized in constructor base/member initializer list
c:\S2_\Tests\C++ Test Wkspce\Test Solution1\Project2\CMammal.h(12) :
see declaration of 'S1::$UnnamedClass$0x512ae669$11$::m_pType2'


However, if I uncomment the line in the constructor that is currently
commented out, all I just get is the following warning:

warning C4608: 'S1::$UnnamedClass$0x512ae669$11$::m_pType2' has already
been initialized by another union member in the initializer list,
'S1::blush:perator`$UnnamedClass$0x512ae669$11$'::S1::$UnnamedClass$0x512ae669$11$::m_pType1'


It seems that the approach I am taking is correct, I should not have to
initialize all the fields of the union, even though they may be const
pointers. And the warning I get if I do that attests to that. However, not
doing so gives a compiler error. Is this compiler error valid?

FYI: the standard says that

A ctor-initializer may initialize the member of an
anonymous union that is a member of the constructor's class. If a
ctor-initializer specifies more than one mem-initializer for the same
member, for the same base class or for multiple members of the same
union (including members of anonymous unions), the ctor-initializer is
ill-formed.

Which I read to mean that you should not initialize more than one member of
an anonymous union in your ctor-initializer.
 
C

Carl Daniel [VC++ MVP]

qwerty2_reverse_iterator said:
Is this a bug with the ms compiler (V7.1)? (It seems so at least.)

I get errors when I don't initialize all the const pointer fields of
an
anonymous union in a struct. Example:

[ code sample snipped ]
It seems that the approach I am taking is correct, I should not have
to
initialize all the fields of the union, even though they may be const
pointers. And the warning I get if I do that attests to that.
However, not
doing so gives a compiler error. Is this compiler error valid?

FYI: the standard says that

A ctor-initializer may initialize the member of an
anonymous union that is a member of the constructor's class. If
a ctor-initializer specifies more than one mem-initializer for the
same member, for the same base class or for multiple members of
the same union (including members of anonymous unions), the
ctor-initializer is ill-formed.

Which I read to mean that you should not initialize more than one
member of
an anonymous union in your ctor-initializer.

Interestingly, Comeau says the code is ill-formed with or without the
commented-out line.

With it commented out it says:

"ComeauTest.c", line 18: error: "S1::S1(const Type1 *)" provides no
initializer for:
const member "S1::m_pType2"
{
^

with it included, the error changes to:

"ComeauTest.c", line 17: error: only one member of a union may be specified
in a
constructor initializer list
, m_pType2((const Type2 * const)pType1)
^

"ComeauTest.c", line 17: warning: type qualifier is meaningless on cast type
, m_pType2((const Type2 * const)pType1)
^
VC8 (Visual Studio 2005) compiles the code without warning with the
initializer for m_pType2 "commented in".

Seems like you've stumbled into a dark corner of the standard, but the VC8
behavior seems like the most useful to me.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top