c++How to write an unsigned short int literal

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

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.

unsigned short bar = (unsigned short) 23;

or in new speak....

unsigned short bar = static_cast<unsigned short>(23);

There are no modifiers for unsigned short. Integers, which has int type by default, usually implicitly converted to target type with no problems. But if you really want to explicitly indicate type, you could write the following:

unsigned short bar = static_cast<unsigned short>(23);

As I can see the only reason is to use such indication for proper deducing template type:

func( static_cast<unsigned short>(23) );

But for such case more clear would be call like the following:

func<unsigned short>( 23 );

Unfortunately, the only method defined for this is

One or two characters in single quotes
('), preceded by the letter L

According to http://cpp.comsci.us/etymology/literals.html

Which means you would have to represent your number as an ASCII escape sequence:

unsigned short bar = L'\x17';

Unfortunately, they can't. But if people just look two words behind the number, they should clearly see it is a short... It's not THAT ambiguous. But it would be nice.

If you express the quantity as a 4-digit hex number, the unsigned shortness might be clearer.

unsigned short bar = 0x0017;

You probably shouldn't use short, unless you have a whole lot of them. It's intended to use less storage than an int, but that int will have the "natural size" for the architecture. Logically it follows that a short probably doesn't. Similar to bitfields, this means that shorts can be considered a space/time tradeoff. That's usually only worth it if it buys you a whole lot of space. You're unlikely to have very many literals in your application, though, so there was no need foreseen to have short literals. The usecases simply didn't overlap.

Related Articles
  • Is there a difference between unsigned short int and a unsigned short decleration in c and if so, what is it please ? I tried looking online, but couldn't find anything worthwhile. unsigned short int x1; unsigned short x2; Lastly, if there is a diffe
  • I have been doing research in google, however I cannot find a solution. How to convert an unsigned short array to a float array in STM32? I need to perform multiplication between an unsigned short array with a float array. To use FPU and DSP library
  • In my program, I often use -1 (aka UINT_MAX) as a value for unsigned variables to denote something special. I also compare against this value. Turning on higher levels of warning messages reveals that compilers (both VS and GCC) do not like this synt
  • I have a string containing the port, when I try to create a tcp endpoint I need its value in unsigned short std::string to_port; .... boost::lexical_cast<unsigned short>(to_port)); throws an exception bad lexical cast: source type value could not be
  • I'm studying signed-unsigned integer conversions and I came to these conclusions, can someone tell me if this is correct please unsigned short var = -65537u; Steps: 65537u (implicitly converted to unsigned int) Binary representation: 0000 0000 0000 0
  • I am trying to write a wrapper make_function, which like std::make_pair can create a std::function object out of suitable callable objects. Just like make_pair, for a function pointer foo, auto f0 = make_function(foo); creates a std::function functio
  • I have some financial data that I am processing in C++. I am storing it in a simple binary format because it requires fewer resources and is fast, however I would like to add compression to the file. I am guessing I will be IO bound so the compressio
  • First of all sorry if this is a duplicate, I couldn't find any subject answering my question. I'm coding a little program that will be used to convert 32-bit floating point values to short int (16 bit) and unsigned char( 8 bits) values. This is for H
  • everyone! I have a 2D vector filled with unsigned chars. Now I want to save its contents into a binary file: std::vector<std::vector<unsigned char> > v2D(1920, std::vector<unsigned char>(1080)); // Populate the 2D vector here ..... FILE*
You Might Also Like