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: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.
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: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.