473,763 Members | 3,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is cast operator unary or binary? How many operands?

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.
Jun 27 '08 #1
16 4717
On Apr 29, 3:45 pm, JoseMariaSola <JoseMariaS...@ gmail.comwrote:
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)(expr ession) 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? */
}
Jun 27 '08 #2
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)(expr ession) 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.
Jun 27 '08 #3
On Apr 29, 1:47 pm, JoseMariaSola <JoseMariaS...@ gmail.comwrote:
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)(expr ession) 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.
Jun 27 '08 #4
In article <5c************ *************** *******@25g2000 hsx.googlegroup s.com>,
JoseMariaSola <Jo***********@ gmail.comwrote:
>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
Jun 27 '08 #5
On Apr 29, 2:18 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
In article <5c21f815-e1e6-468e-8630-cc72dad60...@25 g2000hsx.google groups.com>,

JoseMariaSola <JoseMariaS...@ gmail.comwrote:
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.
Jun 27 '08 #6
JoseMariaSola <Jo***********@ gmail.comwrites :
[...]
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_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #7
In article <13************ *************** *******@x35g200 0hsb.googlegrou ps.com>,
JoseMariaSola <Jo***********@ gmail.comwrote:
>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
Jun 27 '08 #8
On Apr 29, 5:31*pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
In article <13115f63-fd53-4436-879d-4dab6f0fc...@x3 5g2000hsb.googl egroups.com>,

JoseMariaSola *<JoseMariaS... @gmail.comwrote :
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.
Jun 27 '08 #9
In article <f6************ *************** *******@x35g200 0hsb.googlegrou ps.com>,
JoseMariaSola <Jo***********@ gmail.comwrote:
>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
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4995
by: Ali R. | last post by:
Hi guys, I have a string class, which is a wrapper for a char * with a few operators, like + What I am trying to do is reduce the number of overloaded operators so for insteance class String { friend String operator + (const String &Left,const char *Right);
5
1378
by: Gernot Frisch | last post by:
Hi, I can define a class C that can handle a const char* and provide an operator: C::operator string() const; can I define such a cast operator for const char* instead of class C?? --
0
1678
by: A. W. Dunstan | last post by:
I'm porting some code to Visual C++ and have run into a problem - the compiler won't use a user-written cast operator. The code uses an envelope-letter approach to passing (potentially) large pieces of data around, and requires that certain methods return an Envelope with a specific kind of Letter as it's content. I have a cast operator that converts from what I've got to what should be returned, but it seems that the compiler only...
5
1473
by: balor | last post by:
I have a class with a casting operator but GCC doesn't want to cast as necessary and I'm wondering if this is legal or not. class A { public: void test() {} }; class Wrapper { public:
4
1197
by: Dan Plimak | last post by:
Greetings, I'm seeing an odd problem while implementing a template user-specified cast operator for one of my objects which frequently gets static_cast<>ed around. The following test case demonstrates the problem, producing an Internal Compiler Error on the line marked. I'm fairly sure this is valid C++, but I could be wrong. (g++ at least, chows down on this without complaint.) --- snip ---
10
2202
by: Pieter Breed | last post by:
Hi All, Please excuse me, but the bulk of my post will be a code post. It describes some weirdness with regards to the implicit casting operator. The crux of the problem is this: I want to set a property on a class that takes an interface instance. I have a class that can cast implicit to a class that impliments said interface, but the compiler moans and says it cannot cast implicitly like that.
1
2051
by: Muhammad Haseeb | last post by:
plz help me how to use the reinterpret cast operator in filing in c++
2
2763
by: John Goche | last post by:
Hello, Could anyone please provide with some information on the C++ overloaded cast operator and in which circumstances this might be useful? I have consulted several references but found no information on the uses of this operator. Thanks, JG
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9998
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.