Is "upcasting" with a union standards-conforming?

L

luser- -droog

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?
 
I

Ian Collins

luser- -droog said:
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?
 
I

Ian Collins

luser- -droog said:
Copied from a private message by contributor:

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.
 
L

luser- -droog

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.
 
J

James Kuyper

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;
 
E

Eric Sosman

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?

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.
 
T

Tim Rentsch

luser- -droog said:
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?

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.
 
P

Phil Carmody

luser- -droog said:
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. :)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top