C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

some thoughts on anonymous structs.
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Francis Glassborow
Guest





PostPosted: Sun May 02, 2004 11:36 pm    Post subject: some thoughts on anonymous structs. Reply with quote




On one of the other newsgroups the perennial question about why we can
have anonymous unions as class members but not have anonymous structs.
Of course the straight forward answer is that they buy nothing. However
I then went on to think further.

What if we could write:

class myclass {
struct & ;
// rest
};

To declare that reference to an anonymous struct/class. Note that the
compiler does not need a name for this member data because all it has to
do is to provide the mechanism for handling a reference to a
struct/class object.

In the implementation we could have something such as:

myclass::struct {
double d;
int i;
};

and just as we do with unions, we could use the member variable names as
if they were direct members of the enclosing class. This would allow us
to hide the data structure of the class in an implementation file. The
gain is that if performance requires that we remove that layer of
indirection we can do so simply by replacing the anonymous struct with
the actual member declarations. We would not need to touch the
implementation code.

This is very much a raw first set of thoughts but I wonder if it is
worth going further.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects

---
[ 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 ]

Back to top
Niklas Matthies
Guest





PostPosted: Mon May 03, 2004 4:47 am    Post subject: Re: some thoughts on anonymous structs. Reply with quote



On 2004-05-02 23:36, Francis Glassborow wrote:
:
Quote:
What if we could write:

class myclass {
struct & ;
// rest
};
:
In the implementation we could have something such as:

myclass::struct {
double d;
int i;
};

and just as we do with unions, we could use the member variable
names as if they were direct members of the enclosing class.
This would allow us to hide the data structure of the class in an
implementation file. The gain is that if performance requires that
we remove that layer of indirection we can do so simply by replacing
the anonymous struct with the actual member declarations. We would
not need to touch the implementation code.

With some variant of pimpl, you can use the same syntax as well by
making the struct a direct member of the class. E.g. (untested):

Pimpl:

Header:

struct M; // forward declaration

class C {
M & m;
public:
void f();
// rest;
};

Implementation:

struct M { int n; };

void C::f() { ++m.i; }

// rest

Non-pimpl:

Header:

struct M { int n; };

class C {
M m;
public:
void f();
// rest;
};

Implementation:

void C::f() { ++m.i; } // same as with pimpl

Or am I missing something?

-- Niklas Matthies

---
[ 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 ]


Back to top
Marcin 'Qrczak' Kowalczyk
Guest





PostPosted: Mon May 03, 2004 6:05 am    Post subject: Re: some thoughts on anonymous structs. Reply with quote



On Sun, 02 May 2004 23:36:09 +0000, Francis Glassborow wrote:

Quote:
On one of the other newsgroups the perennial question about why we can
have anonymous unions as class members but not have anonymous structs.
Of course the straight forward answer is that they buy nothing.

They buy something when they are used as members of a union. You can't
just splice their contents into the union.

If they were allowed in a union, it would make sense to allow them in a
class or struct for consistency.

--
__("< Marcin Kowalczyk
__/ [email]qrczak (AT) knm (DOT) org.pl[/email]
^^ http://qrnik.knm.org.pl/~qrczak/

---
[ 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 ]


Back to top
llewelly
Guest





PostPosted: Mon May 03, 2004 6:05 am    Post subject: Re: some thoughts on anonymous structs. Reply with quote

[email]francis (AT) robinton (DOT) demon.co.uk[/email] (Francis Glassborow) writes:
[snip]
Quote:
What if we could write:

class myclass {
struct & ;
// rest
};

To declare that reference to an anonymous struct/class. Note that the
compiler does not need a name for this member data because all it has to
do is to provide the mechanism for handling a reference to a
struct/class object.

In the implementation we could have something such as:

myclass::struct {
double d;
int i;
};

and just as we do with unions, we could use the member variable names as
if they were direct members of the enclosing class. This would allow us
to hide the data structure of the class in an implementation file. The
gain is that if performance requires that we remove that layer of
indirection we can do so simply by replacing the anonymous struct with
the actual member declarations. We would not need to touch the
implementation code.

This is very much a raw first set of thoughts but I wonder if it is
worth going further.
[snip]


Could you post a contrast/comparison of this with the compilation
firewall idiom?

---
[ 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 ]


Back to top
Thorsten Ottosen
Guest





PostPosted: Mon May 03, 2004 6:05 am    Post subject: Re: some thoughts on anonymous structs. Reply with quote

"Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote


Quote:
This would allow us
to hide the data structure of the class in an implementation file. The
gain is that if performance requires that we remove that layer of
indirection we can do so simply by replacing the anonymous struct with
the actual member declarations. We would not need to touch the
implementation code.

This is very much a raw first set of thoughts but I wonder if it is
worth going further.

In general it would be great if there were some kind of compiler assistance
with implementation hiding. For example,

class X
{
struct impl_t;
implementation impl_t impl;
};

// X.cpp

struct X::impl_t
{
};

This might work with the restriction that X can only be heap-allocated since it size cannot
be inferred from the header.

just a crasy thought

br

Thorsten


---
[ 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 ]


Back to top
llewelly
Guest





PostPosted: Mon May 03, 2004 4:35 pm    Post subject: Re: some thoughts on anonymous structs. Reply with quote

[email]usenet-nospam (AT) nmhq (DOT) net[/email] (Niklas Matthies) writes:

Quote:
On 2004-05-02 23:36, Francis Glassborow wrote:
:
What if we could write:

class myclass {
struct & ;
// rest
};
:
In the implementation we could have something such as:

myclass::struct {
double d;
int i;
};

and just as we do with unions, we could use the member variable
names as if they were direct members of the enclosing class.
This would allow us to hide the data structure of the class in an
implementation file. The gain is that if performance requires that
we remove that layer of indirection we can do so simply by replacing
the anonymous struct with the actual member declarations. We would
not need to touch the implementation code.

With some variant of pimpl, you can use the same syntax as well by
making the struct a direct member of the class. E.g. (untested):

Pimpl:

Header:

struct M; // forward declaration

You can get closer to Francis's syntax by forward declaring the
struct inside the class.

Quote:
class C {
M & m;
public:
void f();
// rest;
};
[snip]


---
[ 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 ]


Back to top
David Abrahams
Guest





PostPosted: Mon May 03, 2004 4:37 pm    Post subject: Re: some thoughts on anonymous structs. Reply with quote

[email]francis (AT) robinton (DOT) demon.co.uk[/email] (Francis Glassborow) writes:

Quote:
On one of the other newsgroups the perennial question about why we can
have anonymous unions as class members but not have anonymous structs.
Of course the straight forward answer is that they buy nothing. However
I then went on to think further.

What if we could write:

class myclass {
struct & ;
// rest
};

To declare that reference to an anonymous struct/class. Note that the
compiler does not need a name for this member data because all it has to
do is to provide the mechanism for handling a reference to a
struct/class object.

In the implementation we could have something such as:

myclass::struct {
double d;
int i;
};

and just as we do with unions, we could use the member variable names as
if they were direct members of the enclosing class. This would allow us
to hide the data structure of the class in an implementation file. The
gain is that if performance requires that we remove that layer of
indirection we can do so simply by replacing the anonymous struct with
the actual member declarations. We would not need to touch the
implementation code.

So, seeing just the declaration of myclass, how does the compiler
allocate space for

myclass x;

??

Quote:
This is very much a raw first set of thoughts but I wonder if it is
worth going further.

It seems to be totally incompatible with C++'s compilation model.

--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

---
[ 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 ]


Back to top
Graeme Prentice
Guest





PostPosted: Mon May 03, 2004 4:38 pm    Post subject: Re: some thoughts on anonymous structs. Reply with quote

On Sun, 2 May 2004 23:36:09 +0000 (UTC), Francis Glassborow wrote:

Quote:

On one of the other newsgroups the perennial question about why we can
have anonymous unions as class members but not have anonymous structs.
Of course the straight forward answer is that they buy nothing. However
I then went on to think further.


Is there a rationale for the argument that they buy nothing. I had a
need for an anonymous struct recently, as shown in the following code
where I would like to use xflags instead of flags. GCC allows the
anonymous struct and Comeau allows it with a warning but I ended up not
using it because it's non standard. It's not a big deal but I would
have used it if it was standard C++.

(Also I'd like a guarantee that a 16 bit unsigned integral type will
always be around ...)


typedef unsigned short uint16;

struct flags
{
bool f1:1;
bool f2:1;
bool f3:1;
bool f4:1;
// fill up with unused bools to make 16 bits
};

struct flags2
{
union {
uint16 whole;
flags bits;
};
};

struct xflags
{
union {
struct
{
bool f1:1;
bool f2:1;
// ...
};
uint16 whole;
};
};

int main()
{
flags t1,t2;
// ...
// compare t1,t2
flags2 x1,x2;
x1.bits = t1;
x2.bits = t2;
if (x1.whole != x2.whole)
{ // ...
}

xflags s1,s2;
s1.f1 = s2.f1 = true;
// ...
if (s1.whole != s2.whole)
{ // ...
}
}

Graeme

---
[ 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 ]


Back to top
llewelly
Guest





PostPosted: Mon May 03, 2004 4:39 pm    Post subject: Re: some thoughts on anonymous structs. Reply with quote

[email]usenet-nospam (AT) nmhq (DOT) net[/email] (Niklas Matthies) writes:

Quote:
On 2004-05-02 23:36, Francis Glassborow wrote:
:
What if we could write:

class myclass {
struct & ;
// rest
};
:
In the implementation we could have something such as:

myclass::struct {
double d;
int i;
};

and just as we do with unions, we could use the member variable
names as if they were direct members of the enclosing class.
This would allow us to hide the data structure of the class in an
implementation file. The gain is that if performance requires that
we remove that layer of indirection we can do so simply by replacing
the anonymous struct with the actual member declarations. We would
not need to touch the implementation code.

With some variant of pimpl, you can use the same syntax as well by
making the struct a direct member of the class. E.g. (untested):

Pimpl:

Header:

struct M; // forward declaration

You can get closer to Francis's syntax by forward declaring>

Quote:
class C {
M & m;
public:
void f();
// rest;
};
[snip]


---
[ 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 ]


Back to top
Francis Glassborow
Guest





PostPosted: Mon May 03, 2004 6:11 pm    Post subject: Re: some thoughts on anonymous structs. Reply with quote

In article <u4qqy2txk.fsf (AT) boost-consulting (DOT) com>, David Abrahams
<dave (AT) boost-consulting (DOT) com> writes
Quote:
So, seeing just the declaration of myclass, how does the compiler
allocate space for

myclass x;

??

Exactly the same way that it allocates space for:

class example {
struct cat;
cat * smile;
// rest

};


Quote:

This is very much a raw first set of thoughts but I wonder if it is
worth going further.

It seems to be totally incompatible with C++'s compilation mode

Look again and remember that I was just sketching an idea without
filling in too much detail.

There is no magic in the class definition, indeed we could do almost the
same today as above.

The special feature comes in the implementation:

Today we can write:

example::cat {
int i;
double d;
};

example::example(): smile(new cat){}
example::~example(){delete smile;}

and refer to the data as smile->i and smile->d. However if the
programmer decides for performance to remove the compiler firewall and
use:

class example {
int i;
double d;
// rest

};

they now have to touch the whole of the implementation rather than
recompile it. What I am suggesting is that supporting

class example {
struct &;
// rest
};

coupled with allowing the members of an anonymous struct to be referred
to as if they were members of the enclosing class allows the
implementation file to be (almost) lexically independent of whether a
'compiler firewall' is or is not used. If it is, dynamic allocation and
deallocation is automatically provided. If the data members are declared
directly in the class definition everything works as it always has.

I do not think this is completely at variance with the current C++
compilation mode. It simply delegates certain boiler plate code to the
compiler. The problem we have at the moment is that switching between a
class definition using a 'compiler firewall' and one without requires a
good deal of modification to the source code. Note that I am not
proposing link time compatibility, only maximum source level
compatibility.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects

---
[ 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 ]


Back to top
Francis Glassborow
Guest





PostPosted: Mon May 03, 2004 6:26 pm    Post subject: Re: some thoughts on anonymous structs. Reply with quote

In article <cevb90tmical3jnldmelnqt4uqblebrhv2 (AT) 4ax (DOT) com>, Graeme Prentice
<gprenz (AT) hotmail (DOT) com> writes
Quote:
Is there a rationale for the argument that they buy nothing. I had a
need for an anonymous struct recently, as shown in the following code
where I would like to use xflags instead of flags. GCC allows the
anonymous struct and Comeau allows it with a warning but I ended up not
using it because it's non standard. It's not a big deal but I would
have used it if it was standard C++.

(Also I'd like a guarantee that a 16 bit unsigned integral type will
always be around ...)


typedef unsigned short uint16;

struct flags
{
bool f1:1;
bool f2:1;
bool f3:1;
bool f4:1;
// fill up with unused bools to make 16 bits
};
Ah...I had missed the convenience of named bit-fields. Perhaps that does

justify reconsidering anonymous structs.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects

---
[ 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 ]


Back to top
Kurt Bigler
Guest





PostPosted: Tue May 04, 2004 3:29 pm    Post subject: Re: some thoughts on anonymous structs. Reply with quote

in article 5p$BkgdEtolAFw22 (AT) robinton (DOT) demon.co.uk, Francis Glassborow at
[email]francis (AT) robinton (DOT) demon.co.uk[/email] wrote on 5/3/04 11:26 AM:

Quote:
In article <cevb90tmical3jnldmelnqt4uqblebrhv2 (AT) 4ax (DOT) com>, Graeme Prentice
[email]gprenz (AT) hotmail (DOT) com[/email]> writes
Is there a rationale for the argument that they buy nothing.

None that I can see. I never understood the choice.

Quote:
I had a
need for an anonymous struct recently, as shown in the following code
where I would like to use xflags instead of flags. GCC allows the
anonymous struct and Comeau allows it with a warning but I ended up not
using it because it's non standard. It's not a big deal but I would
have used it if it was standard C++.

(Also I'd like a guarantee that a 16 bit unsigned integral type will
always be around ...)


typedef unsigned short uint16;

struct flags
{
bool f1:1;
bool f2:1;
bool f3:1;
bool f4:1;
// fill up with unused bools to make 16 bits
};
Ah...I had missed the convenience of named bit-fields. Perhaps that does
justify reconsidering anonymous structs.

The utility doesn't depend on the use of bit fields. It is just as Marcin
'Qrczak' Kowalczyk said:

in article [email]pan.2004.05.03.00.13.18.990024 (AT) knm (DOT) org.pl[/email], "Marcin 'Qrczak'
Kowalczyk" at [email]qrczak (AT) knm (DOT) org.pl[/email] wrote on 5/2/04 11:05 PM:

Quote:
They buy something when they are used as members of a union. You can't
just splice their contents into the union.

If they were allowed in a union, it would make sense to allow them in a
class or struct for consistency.

This is particularly important if you suddenly realize you need to create a
member that overlays other members in an arbitrary way, but previously there
was only one layer: a struct. Now you need to add an anonymous union and
would love to embed the original struct members (or some of them) in an
anonymous struct within that union so that you don't have to rewrite all
code references to the original member names.

For example:

original implementation:

struct stuff
{
long field1;
long field2;
};

desired enhanced implementation, if only it were possible:

struct stuff
{
long field1;
union
{
long field2;
struct
{
short field2a;
short field2b;
};
};
};

or equally, the original implementation might have been:

struct stuff
{
long field1;
short field2a;
short field2b;
};

and you might have ended up in the same place.

This kind of thing has come up for me several times, once when enhancing an
interpreter in an way entirely unanticipated in the original design.

In fact for me I believe it has come up *every* time I went to use an
anonymous union. Oh boy, I thought, until I realized that anonymous unions
weren't delivering what they promised, because to really do that anonymous
structs are also needed.

-Kurt Bigler

---
[ 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 ]


Back to top
llewelly
Guest





PostPosted: Tue May 04, 2004 3:30 pm    Post subject: Re: some thoughts on anonymous structs. Reply with quote

[email]gprenz (AT) hotmail (DOT) com[/email] (Graeme Prentice) writes:

Quote:
On Sun, 2 May 2004 23:36:09 +0000 (UTC), Francis Glassborow wrote:


On one of the other newsgroups the perennial question about why we can
have anonymous unions as class members but not have anonymous structs.
Of course the straight forward answer is that they buy nothing. However
I then went on to think further.


Is there a rationale for the argument that they buy nothing.

My rationale was that no-one had shown be an example. Now that I've
seen this one, I can appreciate one use.

[snip]
Quote:
struct xflags
{
union {
struct
{
bool f1:1;
bool f2:1;
// ...
};
uint16 whole;
};
};
[snip]


---
[ 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 ]


Back to top
Francis Glassborow
Guest





PostPosted: Tue May 04, 2004 5:31 pm    Post subject: Re: some thoughts on anonymous structs. Reply with quote

In article <BCBC01E7.1452A%kkb (AT) breathsense (DOT) com>, Kurt Bigler
<kkb (AT) breathsense (DOT) com> writes
Quote:
struct stuff
{
long field1;
union
{
long field2;
struct
{
short field2a;
short field2b;
};

Change that to }alias;
and you can write alias.field2a and alias.field2b. That does not seem
very difficult and distinguishes between the levels. Anonymous unions
are used to allow the same block of memory to be referenced by different
names according to the type of data being stored.
Quote:
};
};

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects

---
[ 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 ]


Back to top
Francis Glassborow
Guest





PostPosted: Tue May 04, 2004 6:03 pm    Post subject: Re: some thoughts on anonymous structs. Reply with quote

In article <863c6h117l.fsf (AT) Zorthluthik (DOT) local.bar>, llewelly
<llewelly.at (AT) xmission (DOT) dot.com> writes
Quote:
gprenz (AT) hotmail (DOT) com (Graeme Prentice) writes:

On Sun, 2 May 2004 23:36:09 +0000 (UTC), Francis Glassborow wrote:


On one of the other newsgroups the perennial question about why we can
have anonymous unions as class members but not have anonymous structs.
Of course the straight forward answer is that they buy nothing. However
I then went on to think further.


Is there a rationale for the argument that they buy nothing.

My rationale was that no-one had shown be an example. Now that I've
seen this one, I can appreciate one use.

[snip]
struct xflags
{
union {
struct
{
bool f1:1;
bool f2:1;
// ...
};

But replacing that line with
}field;
supports:
field.f1, field.f2 etc. which seems more expressive to me and a small
penalty in typing.

Quote:
uint16 whole;
};
};

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects

---
[ 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 ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.