1. For types smaller than int, when I compile:
class MyClass
{
static void Main(string[] args)
{
[type] x = 10;
[type] y = -x;
}
}
where [type] is one of {sbyte, byte, short, ushort},
I receive error CS0029: Cannot implicitly convert
type 'int' to 'type'
2. When I compile with int, uint, long, no problem.
3. When I compile using ulong I receive error CS0023:
Operator '-' cannot be applied to operand of type 'ulong'
According to the documentation for the unary operator -
"Unary - operators are predefined for ALL numeric types.
The result of a unary - operation on a numeric type is
the negation of the operand."
Emphasis on ALL is mine.
This documentation is MISLEADING, since section 7.6.2 of
the C# Language Specification states that there are only
two unary operator overloads: for int and long, along
with the conversion rules.
Regards,
Javier
Please respond to the newsgroup so everybody can benefit
from the answer. 2 2371
Joe <jo*@nospam.com > wrote: Per Para 9.4.4.2 of the C# spec, 10 is an integer literal and may be evaluated as int, unint, long or ulong. Therefore, 10 will not evaluate to sbyte, byte, short, or ushort. Per chapter 13 of the spec. assignment of int, unint, long or ulong to sbyte, byte, short, or ushort requires and explicit conversion and you must use the cast operator.
Hmm... I see the bits of the spec you mean, but I was confused as to
why this then compiles and runs without any problem:
using System;
public class Test
{
static void Main()
{
byte b = 10;
Console.WriteLi ne (b);
}
}
And then I found section 14.13, which explains it all, although not
very clearly.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com... Joe <jo*@nospam.com > wrote: Per Para 9.4.4.2 of the C# spec, 10 is an integer literal and may be evaluated as int, unint, long or ulong. Therefore, 10 will not evaluate
to sbyte, byte, short, or ushort. Per chapter 13 of the spec. assignment of int, unint, long or ulong to sbyte, byte, short, or ushort requires and explicit conversion and you must use the cast operator.
Hmm... I see the bits of the spec you mean, but I was confused as to why this then compiles and runs without any problem:
using System;
public class Test { static void Main() { byte b = 10; Console.WriteLi ne (b); } }
And then I found section 14.13, which explains it all, although not very clearly.
Right. ;) The last paragraph of section 14.15 explains why your example is
correct. So, the real error was generated by the results of the unary minus
operator that returned an int. I believe that section 14.13 makes a little
more sense when clarified by section 14.6.2, which defines the return types
of the unary minus operator. BTW, I think the original poster accidently
mistyped the section number 7.6.2, when he really meant 14.6.2.
Joe
-- http://www.csharp-station.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Carlos Ribeiro |
last post by:
I was checking the Prolog recipe in the Cookbook:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303057
It's a clever implementation that explores some aspects of Python that
I wasn't aware of. One of them is the unary plus operator, that calls
the __pos__ method. It's something that can be highly useful in my own
experiments with the use of classes as a tool for generic declarative
descriptions of objects -- UI forms,...
|
by: Elementary Penguin |
last post by:
Is there a left add unary operator ( or do I have to overload one ) ?
For example:
string s1 = "A";
s1 += "B";
Console.WriteLine( s1 )
|
by: Ruben Campos |
last post by:
Some questions about this code:
template <typename T> class MyTemplate;
template <typename T> MyTemplate <T> operator- (const MyTemplate <T> &
object);
template <typename T> MyTemplate <T> operator- (const MyTemplate <T> &
object1, const MyTemplate <T> & object2);
template <typename T>
class MyTemplate
|
by: Anoob |
last post by:
Can we consider () unary operator when calling a function, in exps eq.
f(), ( 1 + 2). But when we call function f( 10 ) it is a binary
operator. Even if we pass f( 10, 20) as we are using , operator there
one operand will be there for ()?
And Unary operators like !, +, -, ~, ... are said to be having
associativity right to left which means that we can write, (but not
allowed)
1. 2!
2. !2
|
by: jimbo |
last post by:
Could someone comment on this code please.
1. Are the comments in it correct?
2. Why does sizeof(arr) not work consistently in C? In someFunction()
sizeof(arr) means sizeof(&arr) in main. The compiler knows how big arr
is, so why the difference - esp. as it would be v.useful if sizeof(arr)
worked in someFunction as it does in main - so you could use sizeof(arr) in
the conditional bit of,say, a for loop in someFunction for example?
| |
by: Marc |
last post by:
Hi,
I've been lurking on clc for a few months now, and want to start by
thanking the regulars here for opening my eyes to a whole new dimension
of "knowing c". Considering I had never even touched the standards a
year ago, though I graduated in embedded SW development...
Anyway, to the problem at hand: I've stumbled upon the following
construct at work recently, and cannot really make up my mind about the
standard's take on the matter.
|
by: lovecreatesbea... |
last post by:
c-faq, 4.3:
The postfix ++ and -- operators essentially have higher precedence
than the prefix unary operators.
BSD Manual, OPERATOR(7):
Operator Associativity
-------- -------------
! ~ ++ -- - (type) * & sizeof right to left
Is there a complete table on operators, their precedence and
|
by: dspfun |
last post by:
I'm trying to get a good understanding of how unary operators work and
have some questions about the following test snippets.
int *p;
~!&*++p--;
It doesn't compile, why? The problem seems to be the ++, the compiler
says: "Error: invalid l-value in increment".
int i = 10;
~!*&i++;
|
by: JoseMariaSola |
last post by:
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.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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...
| |
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |