Topic: proposal: anonymous array members
Author: danielgutson@hotmail.com (danielgutson@hotmail.com)
Date: Fri, 7 Feb 2003 18:34:45 +0000 (UTC) Raw View
Hi again,
I would like to have your feedbacks about this.
Description:
Ability of declaring 'anonymous' array members within
classes/structures.
Example:
struct X
{
int a;
int[20]; // anonymous member
int b;
char[5]; // anonymous member
};
Motivation:
This can be useful for:
- std. way of providing 'holes' inside structures
- readability: clearly saying "I want a padding space here",
don't care about the name you use (sometimes I saw it as '_padding',
as 'dummy', as 'blanco', as 'blank',...)
- forcing a structure of having a fixed length, and ensuring you
don't overgo that size when adding members in the member (see below)
- [definition: anonymous members are not formal members; they
are not initialized nor accessible]. Don't need to initialize them in
the { } initialization as far as they are not accessable
Example using structure X defined above:
struct X x = { 1, 2 };
member 'a' initialized with '1' and member 'b' initialized
with '2'.
Sample application:
-> Forcing a structure to have a fixed length. (Additional rule
required)
Suppose I want to declare a structure Message, of a fixed length of
128 bytes. This structure could have more members in the future (as
the system maintenance continues), but must not exceed this length.
struct Message
{
int member1;
double member2;
char description[50];
};
template <class Content, size_t fixedSize> struct FixedLength :
Content
{
char[ fixedSize - sizeof(Content)];
};
void usage(void)
{
FixedLength<Message,128> msg = { 1, 3.14, "this is a message" };
}
The additional rule required (and also proposed) is that:
structures/classes with single and public inheritance can have '{ }'
initialization iif they have no own members.
This is because (I don't have the standard here, I don't recall how
are they named) derived structs cannot be initialized with '{ }', but
this rule would be an exception: when they have no members and the
inheritance is public and single (according to the definition above,
FixedLength has no own members as far as the anonymous padding is not
a member).
Open ideas: maybe you can suggest a better way of doing this
without inheritance (perhaps something with anonymous structures?).
Listening comments.
Compatibility:
This proposal is an invalid syntax in current C++, therefore
provides full compatibility.
Thanks!
Daniel.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]