Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is "upcasting" with a union standards-conforming?

114 views
Skip to first unread message

luser- -droog

unread,
Nov 11, 2013, 1:22:31 AM11/11/13
to
I just learned that the suncc compiler does not accept code that I thought was fine (and really cool). Have I been unknowingly using a gcc extension?

The union type is defined in src/lib/object.h
http://code.google.com/p/xpost/source/browse/src/lib/xpost_object.h#352

typedef union
{
word tag;

Xpost_Object_Mark mark_;
Xpost_Object_Int int_;
Xpost_Object_Real real_;
Xpost_Object_Extended extended_;
Xpost_Object_Comp comp_;
Xpost_Object_Save save_;
Xpost_Object_Saverec saverec_;
Xpost_Object_Glob glob_;
Xpost_Object_Magic magic_;
} Xpost_Object;

And the suncc compiler didn't like this:
(concise example, relevant changelog:
http://code.google.com/p/xpost/source/detail?r=60c9fc1e7e100ef3ecd2b93c164711aa0cbf17f2)

Xpost_Object_Extended e;
Xpost_Object o;
o = (Xpost_Object)e;

But I really like doing this! It makes me feel like
I'm special. Some kind of OO-ninja. :)

Does my ninjitsu not follow the qi?

Ian Collins

unread,
Nov 11, 2013, 2:20:28 AM11/11/13
to
luser- -droog wrote:
> I just learned that the suncc compiler does not accept code that I
> thought was fine (and really cool). Have I been unknowingly using a
> gcc extension?


What is the error?

--
Ian Collins

luser- -droog

unread,
Nov 11, 2013, 2:32:21 AM11/11/13
to
Copied from a private message by contributor:

> note that without that, suncc fails (it also fails similarly there : "src/bin/xpost_operator.c", line 126: invalid cast expression)

Ian Collins

unread,
Nov 11, 2013, 2:45:07 AM11/11/13
to
Please clean up the awful mess that shite google interface makes of you
quotes!

gcc will also whinge at the code in conforming mode, casting to a union
isn't allowed.

--
Ian Collins

luser- -droog

unread,
Nov 11, 2013, 2:56:49 AM11/11/13
to
On Monday, November 11, 2013 1:45:07 AM UTC-6, Ian Collins wrote:
> luser- -droog wrote:
> > On Monday, November 11, 2013 1:20:28 AM UTC-6, Ian Collins wrote:
> >> luser- -droog wrote:
> >>> I just learned that the suncc compiler does not accept code that
> >>> I thought was fine (and really cool). Have I been unknowingly using
> >>> a gcc extension?

> >> What is the error?

> > Copied from a private message by contributor:
> >> note that without that, suncc fails (it also fails similarly there
> >> : "src/bin/xpost_operator.c", line 126: invalid cast expression)
>
>
> Please clean up the awful mess that shite google interface makes of you
> quotes!

I did! I know. :(

> gcc will also whinge at the code in conforming mode, casting to a union
> isn't allowed.
>

Understood. Lesson learned. Thank you for the help.
I see also that I might've discovered the answer myself.

James Kuyper

unread,
Nov 11, 2013, 7:12:16 AM11/11/13
to
On 11/11/2013 01:22 AM, luser- -droog wrote:
> I just learned that the suncc compiler does not accept code that I thought was fine (and really cool). Have I been unknowingly using a gcc extension?
>
> The union type is defined in src/lib/object.h
> http://code.google.com/p/xpost/source/browse/src/lib/xpost_object.h#352
>
> typedef union
> {
> word tag;
>
> Xpost_Object_Mark mark_;
> Xpost_Object_Int int_;
> Xpost_Object_Real real_;
> Xpost_Object_Extended extended_;
> Xpost_Object_Comp comp_;
> Xpost_Object_Save save_;
> Xpost_Object_Saverec saverec_;
> Xpost_Object_Glob glob_;
> Xpost_Object_Magic magic_;
> } Xpost_Object;
>
> And the suncc compiler didn't like this:
> (concise example, relevant changelog:
> http://code.google.com/p/xpost/source/detail?r=60c9fc1e7e100ef3ecd2b93c164711aa0cbf17f2)
>
> Xpost_Object_Extended e;
> Xpost_Object o;
> o = (Xpost_Object)e;

When you do a cast, "... the type name shall specify atomic, qualified,
or unqualified scalar type, ..." (6.5.4p2) Xpost_Object is not a scalar
type.

Why not use:
o.extended_ = e;
--
James Kuyper

Eric Sosman

unread,
Nov 11, 2013, 8:04:43 AM11/11/13
to
It does not. 6.5.4: "Unless the type name specifies a void type,
the type name shall specify atomic, qualified, or unqualified scalar
type, and the operand shall have scalar type." Since Xpost_Object is
not a scalar type (and we assume Xpost_Object_Extended isn't, either),
the code violates a "shall" -- that happens to be in a "constraints"
section, so the compiler is required to protest.

--
Eric Sosman
eso...@comcast-dot-net.invalid

Tim Rentsch

unread,
Nov 12, 2013, 7:48:00 AM11/12/13
to
The cast isn't allowed in standard C, as you have discovered.
However, if you're using C99, you can do this:

(Xpost_Object){ .extended_ = e }

This expression produces an Xpost_Object value, and also is type
safe.

Phil Carmody

unread,
Nov 14, 2013, 6:29:09 PM11/14/13
to
It's fucking awful code. Casts are very often a warning sign. This is
flashing bright orange, with sirens blaring. What's wrong with:

o.extended_ = e;

> Does my ninjitsu not follow the qi?

Have you considered the possibility that you're not a ninja but a bodger?

Phil
--
The list of trusted root authorities in your browser included the
United Arab Emirates-based Etisalat, which was caught secretly
uploading spyware onto 100,000 customers' BlackBerries.
http://www.wired.com/threatlevel/2009/07/blackberry-spies/
0 new messages