J
jmucchiello
I posted this to comp.std.c++ last week and it never showed up so I'll
post it here to see if there's any interest. Sorry if that's against
the rules but I don't know where a FAQ might be.
-------
Hi, I have a proposal for a language addition. I realize it is far
too late
for this idea in C++0x but I think it would be a good addition to the
language.
My proposal is to make it possible to explicitly mark a struct (or
class) as
plain-old-data (POD). Currently, the language specification states the
rules for
determining IF a struct (or class) is POD. While these rules were
relaxed in
C++0x, the objective of this proposal is to allow the designer to
force a struct
to be POD and to allow the compiler to output errors if the
requirement is
violated.
As with any proposal for language change there comes the issue of
syntax or
keyword usage. This proposal follows the hallowed tradition of piling
yet
another meaning onto the 'static' keyword. To mark a struct as POD one
adds the
static keyword after the struct (or class) keyword:
struct static Foo { ... };
Once marked as POD, any construct that would make it impossible
for the
struct to be POD MUST be flagged as an error by the compiler:
class Foo {
public:
virtual int f() {}
};
struct static Bar : public Foo { ... }; // error Bar cannot
contain
// virtual functions
struct static Baz {
int x;
Foo foo; // error, POD structs cannot contain non-POD elements
};
struct static Boo { ... };
class Goo : public Boo {
public:
virtual int f() {} // this is legal, Goo is not POD
};
That's it in a nutshell. A few anticipated concerns are addressed
below:
Does C++ need this?
Do we need anything? Seriously, every version of C++'s language
specification spent a section or two describing what POD is and how to
achieve
it for backward compatibility with C and system calls. Having a way
for the
compiler to know what MUST be POD and tell you when you have caused a
struct to
exit the realm of POD must be a useful concept. Frankly, I'm surprised
it
doesn't already exist.
Couldn't attributes be used instead of overloading static yet again?
My understanding of attributes is that they are helpers to the
compiler. The
whole point of adding POD requirements to struct is as a help to the
library
developer. So while it could be done with attributes (as could default
and
deleted constructors, for example), attributes speak to the compiler.
Here, the
library designer is speaking to the library user.
Should POD status affect name-mangling?
This is not a lang-spec issue really. It would be nice since it
would ensure
the POD status of the struct across separate compilations. It would
also break
existing ABIs however that should not be an issue since moving from C+
+03 to
C++0x will also break existing ABIs I suspect.
Does this impact compatibility with C?
I suspect that if this proposal were accepted as part of C++ a lot
of existing
headers would be modified using a #define that is defined as empty
when the
compiler is in C mode and is defined as static when in C++ mode:
#ifdef __CPLUSPLUS
#define __POD__ static
#else
#define __POD__
#endif
struct __POD__ FILELAYOUT { ... };
Thanks for listening. I'll now stand back and see where I've gone
astray.
Joe Mucchiello
post it here to see if there's any interest. Sorry if that's against
the rules but I don't know where a FAQ might be.
-------
Hi, I have a proposal for a language addition. I realize it is far
too late
for this idea in C++0x but I think it would be a good addition to the
language.
My proposal is to make it possible to explicitly mark a struct (or
class) as
plain-old-data (POD). Currently, the language specification states the
rules for
determining IF a struct (or class) is POD. While these rules were
relaxed in
C++0x, the objective of this proposal is to allow the designer to
force a struct
to be POD and to allow the compiler to output errors if the
requirement is
violated.
As with any proposal for language change there comes the issue of
syntax or
keyword usage. This proposal follows the hallowed tradition of piling
yet
another meaning onto the 'static' keyword. To mark a struct as POD one
adds the
static keyword after the struct (or class) keyword:
struct static Foo { ... };
Once marked as POD, any construct that would make it impossible
for the
struct to be POD MUST be flagged as an error by the compiler:
class Foo {
public:
virtual int f() {}
};
struct static Bar : public Foo { ... }; // error Bar cannot
contain
// virtual functions
struct static Baz {
int x;
Foo foo; // error, POD structs cannot contain non-POD elements
};
struct static Boo { ... };
class Goo : public Boo {
public:
virtual int f() {} // this is legal, Goo is not POD
};
That's it in a nutshell. A few anticipated concerns are addressed
below:
Does C++ need this?
Do we need anything? Seriously, every version of C++'s language
specification spent a section or two describing what POD is and how to
achieve
it for backward compatibility with C and system calls. Having a way
for the
compiler to know what MUST be POD and tell you when you have caused a
struct to
exit the realm of POD must be a useful concept. Frankly, I'm surprised
it
doesn't already exist.
Couldn't attributes be used instead of overloading static yet again?
My understanding of attributes is that they are helpers to the
compiler. The
whole point of adding POD requirements to struct is as a help to the
library
developer. So while it could be done with attributes (as could default
and
deleted constructors, for example), attributes speak to the compiler.
Here, the
library designer is speaking to the library user.
Should POD status affect name-mangling?
This is not a lang-spec issue really. It would be nice since it
would ensure
the POD status of the struct across separate compilations. It would
also break
existing ABIs however that should not be an issue since moving from C+
+03 to
C++0x will also break existing ABIs I suspect.
Does this impact compatibility with C?
I suspect that if this proposal were accepted as part of C++ a lot
of existing
headers would be modified using a #define that is defined as empty
when the
compiler is in C mode and is defined as static when in C++ mode:
#ifdef __CPLUSPLUS
#define __POD__ static
#else
#define __POD__
#endif
struct __POD__ FILELAYOUT { ... };
Thanks for listening. I'll now stand back and see where I've gone
astray.
Joe Mucchiello