Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an unsigned short int literal?

Tags:

c++

types

42 as unsigned int is well defined as "42U".

unsigned int foo = 42U; // yeah! 

How can I write "23" so that it is clear it is an unsigned short int?

unsigned short bar = 23; // booh! not clear! 

EDIT so that the meaning of the question is more clear:

template <class T> void doSomething(T) {     std::cout << "unknown type" << std::endl; }  template<> void doSomething(unsigned int) {     std::cout << "unsigned int" << std::endl; }  template<> void doSomething(unsigned short) {     std::cout << "unsigned short" << std::endl; }  int main(int argc, char* argv[]) {     doSomething(42U);     doSomething((unsigned short)23); // no other option than a cast?      return EXIT_SUCCESS; } 
like image 366
moala Avatar asked Aug 25 '09 15:08

moala


People also ask

What is unsigned short int?

It is the smallest (16 bit) integer data type in C++. Some properties of the unsigned short int data type are: Being an unsigned data type, it can store only positive values. Takes a size of 16 bits.

Which are unsigned int literals?

There are no negative integer literals. Expressions such as -1 apply the unary minus operator to the value represented by the literal, which may involve implicit type conversions. In C prior to C99 (but not in C++), unsuffixed decimal values that do not fit in long int are allowed to have the type unsigned long int.

How do you write short in C++?

short type Modifier We can use short for small integers (in the range −32,767 to 32,767). For example, // small integer short a = 12345; Here, a is a short integer variable.


2 Answers

You can't. Numeric literals cannot have short or unsigned short type.

Of course in order to assign to bar, the value of the literal is implicitly converted to unsigned short. In your first sample code, you could make that conversion explicit with a cast, but I think it's pretty obvious already what conversion will take place. Casting is potentially worse, since with some compilers it will quell any warnings that would be issued if the literal value is outside the range of an unsigned short. Then again, if you want to use such a value for a good reason, then quelling the warnings is good.

In the example in your edit, where it happens to be a template function rather than an overloaded function, you do have an alternative to a cast: do_something<unsigned short>(23). With an overloaded function, you could still avoid a cast with:

void (*f)(unsigned short) = &do_something; f(23); 

... but I don't advise it. If nothing else, this only works if the unsigned short version actually exists, whereas a call with the cast performs the usual overload resolution to find the most compatible version available.

like image 119
Steve Jessop Avatar answered Sep 18 '22 15:09

Steve Jessop


unsigned short bar = (unsigned short) 23; 

or in new speak....

unsigned short bar = static_cast<unsigned short>(23); 
like image 24
AnthonyLambert Avatar answered Sep 20 '22 15:09

AnthonyLambert