Velocity Reviews > Is cast operator unary or binary? How many operands?

# Is cast operator unary or binary? How many operands?

JoseMariaSola
Guest
Posts: n/a

 04-29-2008
How may operators and operands does (typename) expression has?

I'd say one operator, the cast operator, and two operands: typename
and expression.

But everywhere I read, cast is categorized as an unary operator. Why
is that? Is it just a syntax cotegory?

Thanks.

José María.

vippstar@gmail.com
Guest
Posts: n/a

 04-29-2008
On Apr 29, 3:45 pm, JoseMariaSola <(E-Mail Removed)> wrote:
> How may operators and operands does (typename) expression has?
>
> I'd say one operator, the cast operator, and two operands: typename
> and expression.
>
> But everywhere I read, cast is categorized as an unary operator. Why
> is that? Is it just a syntax cotegory?

(typename)(expression) has one operator `(typename)' and one operand
`(expression)'.
The reason the type is enclosed in parentheses (as a design decision)
is probably to avoid ambiguity, consider this:

int i = 1; /* define and initialize i to 1 */
{
int i; /* cast i to int, a statement with no effect, or define i in
block scope? */
}

JoseMariaSola
Guest
Posts: n/a

 04-29-2008
> > I'd say one operator, the cast operator, and two operands: typename
> > and expression.

>
> > But everywhere I read, cast is categorized as an unary operator. Why
> > is that? Is it just a syntax cotegory?

>
> (typename)(expression) has one operator `(typename)' and one operand
> `(expression)'.
> The reason the type is enclosed in parentheses (as a design decision)
> is probably to avoid ambiguity, consider this:
>
> int i = 1; /* define and initialize i to 1 */
> {
> int i; /* cast i to int, a statement with no effect, or define i in
> block scope? */
>
> }

Thanks, Vipps.

According to your answeer, the operator '(typename)' is very
particular, because it not a single token but three AND the middle
token is anything an identifier may be.

JM.

JoseMariaSola
Guest
Posts: n/a

 04-29-2008
On Apr 29, 1:47 pm, JoseMariaSola <(E-Mail Removed)> wrote:
> > > I'd say one operator, the cast operator, and two operands: typename
> > > and expression.

>
> > > But everywhere I read, cast is categorized as an unary operator. Why
> > > is that? Is it just a syntax cotegory?

>
> > (typename)(expression) has one operator `(typename)' and one operand
> > `(expression)'.
> > The reason the type is enclosed in parentheses (as a design decision)
> > is probably to avoid ambiguity, consider this:

>
> > int i = 1; /* define and initialize i to 1 */
> > {
> > int i; /* cast i to int, a statement with no effect, or define i in
> > block scope? */

>
> > }

>
> Thanks, Vipps.
>
> According to your answeer, the operator '(typename)' is very
> particular, because it not a single token but three AND the middle
> token is anything an identifier may be.
>
> JM.

The last line of my last post shoudl be:
... the middle token is anything an identifier may be and more.

Here is part of the grammar:

unary-expression:
postfix-expression
++ unary-expression
-- unary-expression
unary-operator cast-expression
sizeof unary-expression
sizeof ( type-name )

unary-operator: one of
& * + - ˜ !

cast-expression:
unary-expression
( type-name ) cast-expression

Why sizeof, (type-name), ++ and -- aren't unary-operators?

Thanks.

Richard Tobin
Guest
Posts: n/a

 04-29-2008
In article <(E-Mail Removed)>,
JoseMariaSola <(E-Mail Removed)> wrote:

>Here is part of the grammar:
>
>unary-expression:
> postfix-expression
> ++ unary-expression
> -- unary-expression
> unary-operator cast-expression
> sizeof unary-expression
> sizeof ( type-name )
>
>unary-operator: one of
> & * + - ˜ !
>
>cast-expression:
> unary-expression
> ( type-name ) cast-expression
>
>
>Why sizeof, (type-name), ++ and -- aren't unary-operators?

You could include sizeof in unary-operator, but you'd still need the
special case of a type-name operand requiring parentheses, and it
seems clearer to keep them together in the list. Similarly ++ and --
would still have to appear in postfix-expression. Both of these are
just matters of taste really. I don't see how you could do
(type-name), because a unary-operator is something that precedes its
operand, and the parentheses of a cast have to go around the operand.

-- Richard

--
:wq

JoseMariaSola
Guest
Posts: n/a

 04-29-2008
On Apr 29, 2:18 pm, (E-Mail Removed) (Richard Tobin) wrote:
> In article <(E-Mail Removed)>,
>
>
>
> JoseMariaSola <(E-Mail Removed)> wrote:
> >Here is part of the grammar:

>
> >unary-expression:
> > postfix-expression
> > ++ unary-expression
> > -- unary-expression
> > unary-operator cast-expression
> > sizeof unary-expression
> > sizeof ( type-name )

>
> >unary-operator: one of
> > & * + - ˜ !

>
> >cast-expression:
> > unary-expression
> > ( type-name ) cast-expression

>
> >Why sizeof, (type-name), ++ and -- aren't unary-operators?

>
> You could include sizeof in unary-operator, but you'd still need the
> special case of a type-name operand requiring parentheses, and it
> seems clearer to keep them together in the list. Similarly ++ and --
> would still have to appear in postfix-expression. Both of these are
> just matters of taste really.
> I don't see how you could do
> (type-name), because a unary-operator is something that precedes its
> operand, and the parentheses of a cast have to go around the operand.

But in the case of cast the (only) operand is the expression the left,
not the type-name.

Talking with you both I notice that every operator requires
expressions as operands, and type-name is not an expression. Am I
right?

JM.

Keith Thompson
Guest
Posts: n/a

 04-29-2008
JoseMariaSola <(E-Mail Removed)> writes:
[...]
> Talking with you both I notice that every operator requires
> expressions as operands, and type-name is not an expression. Am I
> right?

Ideally, yes, but C is not an ideal language (not that I'm arguing it
needs to be).

"sizeof" is a unary operator; its operand can be either an expression
(specifically a unary-expression) or a parenthesized type name.

Or, if you prefer, "sizeof ( type-name )" is a special form of
expression. You don't *have* to think of the parenthesized type name
as an operand. But the standard puts it in the same section with the
real unary operators, probably because the other form of "sizeof" is
in that section. It would have been more logical, but less clear, to
separate them.

"." and "->" could be thought of as binary operators whose right
operand is an identifier, but the standard treats them as postfix
operators. Given:
struct foo { int x; int y; };
you can think of ".x" and ".y" as two distinct postfix operators, both
applicable to operands of type "struct foo".

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Richard Tobin
Guest
Posts: n/a

 04-29-2008
In article <(E-Mail Removed)>,
JoseMariaSola <(E-Mail Removed)> wrote:

>But in the case of cast the (only) operand is the expression the left,
>not the type-name.

I see, you want to consider, say, (int) as an operator with a single
operand, rather than (int)x containing an operator with two operands,
"int" and "x".

This would imply an infinite number of operators, which is not
out of the question but doesn't seem to offer any advantage.

>Talking with you both I notice that every operator requires
>expressions as operands, and type-name is not an expression. Am I
>right?

The two traditional uses of "( type-name )" - in casts and sizeof -
and the new use in C99 - in compound literals - are indeed
exceptional. I previously suggested that we could factor out

type-expression:
( type-name )

which would make sizeof in particular more regular:

unary-expression:
...
sizeof unary-expression
sizeof type-expression

-- Richard
--
:wq

JoseMariaSola
Guest
Posts: n/a

 04-30-2008
On Apr 29, 5:31*pm, (E-Mail Removed) (Richard Tobin) wrote:
> In article <(E-Mail Removed)>,
>
> JoseMariaSola *<(E-Mail Removed)> wrote:
> >But in the case of cast the (only) operand is the expression the left,
> >not the type-name.

>
> I see, you want to consider, say, (int) as an operator with a single
> operand, rather than (int)x containing an operator with two operands,
> "int" and "x".
>
> This would imply an infinite number of operators, which is not
> out of the question but doesn't seem to offer any advantage.
>
> >Talking with you both I notice that every operator requires
> >expressions as operands, and type-name is not an expression. Am I
> >right?

>
> The two traditional uses of "( type-name )" - in casts and sizeof -
> and the new use in C99 - in compound literals - are indeed
> exceptional. *I previously suggested that we could factor out
>
> * type-expression:
> * * *( type-name )
>
> which would make sizeof in particular more regular:
>
> * unary-expression:
> * * *...
> * * *sizeof unary-expression
> * * *sizeof type-expression
>
> -- Richard
> --
> :wq

Thanks, Richard.

So both following expressions have one operator and one operand?
sizeof(int)
(int)x

typename is considered an operand in the firs expressin but not in the
second one?

JM.

Richard Tobin
Guest
Posts: n/a

 04-30-2008
In article <(E-Mail Removed)>,
JoseMariaSola <(E-Mail Removed)> wrote:

>> >But in the case of cast the (only) operand is the expression the left,
>> >not the type-name.

[...]

>So both following expressions have one operator and one operand?
> sizeof(int)
> (int)x

*I* dont' consider it that way. I was just exploring the consequences
of your suggestion. I would say that they have one and two operands
respectively.

-- Richard
--
:wq