Re: inhibit compiler warning C4624 for a class hierarchy
"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:OMhw3kbOHHA.5000@TK2MSFTNGP03.phx.gbl...
Ben Voigt <rbv@nospam.nospam> wrote:
If a class does not have a
trivial constructor, does it necessarily have a non-trivial
constructor, or can it have no constructor at all?
A class has either a trivial or a non-trivial constructor. There ain't no
such thing as a class with no declared constructor. It's possible to have
a constructor that is declared but not defined: an object with such a
constructor can never be instantiated.
By the way, you keep mentioning "a ... hierarchy of POD classes". Such a
thing does not exist:
The example I gave, Win32's ENHMETARECORD, is a hierarchy of POD types,
having what the standard refers to as layout compatibility for initial
members. Apparently a union must be used to access such data, and casting
between the structs isn't strictly allowed. Also the amount of redundancy
in the type definitions is tremendous, with no easy way to check initial
layout compatibility. That's why I was looking for a better way to design
my own hierarchy using C++ features like inheritance and templates.
9/4 A POD-struct is an aggregate class...
8.5.1/1 An aggregate is ... a class (clause 9) with no user-declared
constructors (12.1), no private or protected non-static data members
(clause 11), no base classes (clause 10), and no virtual functions (10.3).
It appears that none of your classes are actually POD types. Luckily, you
don't need to meet a higher bar of using only POD types: it should be
sufficient for your purposes to have types with trivial constructor and
destructor. In particular, you can use inheritance, but you shouldn't have
any user-declared constructors or destructors.
.... as well all non-static data members should have trivial constructors and
destructors, which is why today I wrote "the hierarchy will permit only POD
members"... which seems to be an essential part of the lower bar you speak
of.
It's too bad C++ doesn't have any concept of typesafe varargs in templates.
It would be really nice to be able to do something like (without having to
define a ton of overloads for f with different argument count and template
argument lists):
struct A
{
void init(int);
void doit();
void doit(int, char, double);
void printresults();
};
template<typelist S>
void f(int i, S arglist)
{
A a;
a.init(i);
a.doit(arglist);
a.printresults();
}
f(1); // calls A::doit(void)
f(1, 2, 'X', 3.14159); // calls A::doit(int, char, double)
What's also needed is a noinherit keyword for class members that affects
name visibility, especially overload resolution, but not access:
struct X
{
noinherit static int x;
static int y;
noinherit enum { xxx = 1; }
noinherit int f(int);
};
struct Y : public X
{
void f(double);
void test() {
x; // not allowed
X::x; // ok
xxx; // not allowed
__super::xxx; // ok
f(1); // calls Y::f(double)
}
};
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925