public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* compile error when initializing descendant struct
@ 2008-04-09 23:09 Dave Bender
  2008-04-09 23:42 ` Young, Michael
  2008-04-10  2:42 ` corey taylor
  0 siblings, 2 replies; 6+ messages in thread
From: Dave Bender @ 2008-04-09 23:09 UTC (permalink / raw)
  To: gcc-help

Dear list,
  When I attempt to compile the following code (test2.cpp):


struct A {
  int x;
};

struct B : public A {
  int y;
};

const B b = {1, 2};


g++ gives the following error:

test2.cpp:9: error: scalar object 'b' requires one element in initializer

My goal is to incorporate a large set of structs in a DSO (so that
they reside in the read only section). I don't see why this code
should not compile.

Thanks,
-Dave

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: compile error when initializing descendant struct
  2008-04-09 23:09 compile error when initializing descendant struct Dave Bender
@ 2008-04-09 23:42 ` Young, Michael
  2008-04-10  2:09   ` Dave Bender
  2008-04-10  2:42 ` corey taylor
  1 sibling, 1 reply; 6+ messages in thread
From: Young, Michael @ 2008-04-09 23:42 UTC (permalink / raw)
  To: Dave Bender, gcc-help

Maybe I'm wrong, but this doesn't appear to be valid C++ to me... you're
using a construct that's normally used for array initialization - I
don't think it's valid for initializing an object (instead, use a ctor).
Object layout is not guaranteed to be the same as an "array" of the data
members (even if the members are the same datatype, except for perhaps
PODs), but even if it the same, I don't believe you can use array
initialization syntax just because the memory layout is the same.

Here's what I think you want to do:

struct A {
  int x;
};

struct B : public A {

  B( int arg_x, int arg_y ) : y( arg_y ) { x = arg_x ; }

  int y;
};

const B b( 1, 2 );

I don't have g++ working on this machine at the moment, but this at
least compiles on another C++ compiler.
Note that you could also give A a ctor that initializes x, and then
initialize the base class in the initialization list of B ctor (instead
of doing the assignment of x in the body of B's ctor).

HTH,
  Mike

-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Dave Bender
Sent: Wednesday, April 09, 2008 6:07 PM
To: gcc-help@gcc.gnu.org
Subject: compile error when initializing descendant struct

Dear list,
  When I attempt to compile the following code (test2.cpp):


struct A {
  int x;
};

struct B : public A {
  int y;
};

const B b = {1, 2};


g++ gives the following error:

test2.cpp:9: error: scalar object 'b' requires one element in
initializer

My goal is to incorporate a large set of structs in a DSO (so that they
reside in the read only section). I don't see why this code should not
compile.

Thanks,
-Dave

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: compile error when initializing descendant struct
  2008-04-09 23:42 ` Young, Michael
@ 2008-04-10  2:09   ` Dave Bender
  2008-04-10  3:18     ` Brian Dessent
  0 siblings, 1 reply; 6+ messages in thread
From: Dave Bender @ 2008-04-10  2:09 UTC (permalink / raw)
  To: Young, Michael; +Cc: gcc-help

Mike,
   In C++, structs can be initialized in such a way: please see
section 5.7 in TC++PL.

 Although your solution is correct, I want to instantiate the structs
in reside in the .rodata section of the DSO,
which cannot be done by using the output of a function call.

Dave

On Wed, Apr 9, 2008 at 6:34 PM, Young, Michael <Michael.Young@paetec.com> wrote:
> Maybe I'm wrong, but this doesn't appear to be valid C++ to me... you're
>  using a construct that's normally used for array initialization - I
>  don't think it's valid for initializing an object (instead, use a ctor).
>  Object layout is not guaranteed to be the same as an "array" of the data
>  members (even if the members are the same datatype, except for perhaps
>  PODs), but even if it the same, I don't believe you can use array
>  initialization syntax just because the memory layout is the same.
>
>  Here's what I think you want to do:
>
>
>  struct A {
>   int x;
>  };
>
>  struct B : public A {
>
>   B( int arg_x, int arg_y ) : y( arg_y ) { x = arg_x ; }
>
>   int y;
>  };
>
>  const B b( 1, 2 );
>
>  I don't have g++ working on this machine at the moment, but this at
>  least compiles on another C++ compiler.
>  Note that you could also give A a ctor that initializes x, and then
>  initialize the base class in the initialization list of B ctor (instead
>  of doing the assignment of x in the body of B's ctor).
>
>  HTH,
>   Mike
>
>
>
>  -----Original Message-----
>  From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
>  Behalf Of Dave Bender
>  Sent: Wednesday, April 09, 2008 6:07 PM
>  To: gcc-help@gcc.gnu.org
>  Subject: compile error when initializing descendant struct
>
>  Dear list,
>   When I attempt to compile the following code (test2.cpp):
>
>
>  struct A {
>   int x;
>  };
>
>  struct B : public A {
>   int y;
>  };
>
>  const B b = {1, 2};
>
>
>  g++ gives the following error:
>
>  test2.cpp:9: error: scalar object 'b' requires one element in
>  initializer
>
>  My goal is to incorporate a large set of structs in a DSO (so that they
>  reside in the read only section). I don't see why this code should not
>  compile.
>
>  Thanks,
>  -Dave
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: compile error when initializing descendant struct
  2008-04-09 23:09 compile error when initializing descendant struct Dave Bender
  2008-04-09 23:42 ` Young, Michael
@ 2008-04-10  2:42 ` corey taylor
  2008-04-10  3:21   ` Dave Bender
  1 sibling, 1 reply; 6+ messages in thread
From: corey taylor @ 2008-04-10  2:42 UTC (permalink / raw)
  To: Dave Bender; +Cc: gcc-help

On Wed, Apr 9, 2008 at 6:06 PM, Dave Bender <codehero@gmail.com> wrote:
> struct B : public A {
>  int y;
> };
>
> My goal is to incorporate a large set of structs in a DSO (so that
> they reside in the read only section). I don't see why this code
> should not compile.

Have you considered using composition and then initializing A then y
in your aggregate?

corey

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: compile error when initializing descendant struct
  2008-04-10  2:09   ` Dave Bender
@ 2008-04-10  3:18     ` Brian Dessent
  0 siblings, 0 replies; 6+ messages in thread
From: Brian Dessent @ 2008-04-10  3:18 UTC (permalink / raw)
  To: Dave Bender; +Cc: Young, Michael, gcc-help

Dave Bender wrote:

>  Although your solution is correct, I want to instantiate the structs
> in reside in the .rodata section of the DSO,
> which cannot be done by using the output of a function call.

I don't think it's possible, since having a base class makes struct B
non-POD.  There might be hope in C++0x, where the POD type rules have
been revisited, but that's still a draft and from the status page it's
not implemented in any gcc yet:
<http://gcc.gnu.org/projects/cxx0x.html>.

Brian

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: compile error when initializing descendant struct
  2008-04-10  2:42 ` corey taylor
@ 2008-04-10  3:21   ` Dave Bender
  0 siblings, 0 replies; 6+ messages in thread
From: Dave Bender @ 2008-04-10  3:21 UTC (permalink / raw)
  To: corey taylor; +Cc: gcc-help

Corey,
  That is a good suggestion and likely what I will end up doing. I
wanted to be able to cast between B and A but the read only storage is
more important.

Dave

On Wed, Apr 9, 2008 at 9:53 PM, corey taylor <corey.taylor@gmail.com> wrote:
> On Wed, Apr 9, 2008 at 6:06 PM, Dave Bender <codehero@gmail.com> wrote:
>  > struct B : public A {
>  >  int y;
>  > };
>  >
>
> > My goal is to incorporate a large set of structs in a DSO (so that
>  > they reside in the read only section). I don't see why this code
>  > should not compile.
>
>  Have you considered using composition and then initializing A then y
>  in your aggregate?
>
>  corey
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2008-04-10  2:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-09 23:09 compile error when initializing descendant struct Dave Bender
2008-04-09 23:42 ` Young, Michael
2008-04-10  2:09   ` Dave Bender
2008-04-10  3:18     ` Brian Dessent
2008-04-10  2:42 ` corey taylor
2008-04-10  3:21   ` Dave Bender

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).