How do I write a short literal in C++?

C++Literals

C++ Problem Overview


Very basic question: how do I write a short literal in C++?

I know the following:

  • 2 is an int
  • 2U is an unsigned int
  • 2L is a long
  • 2LL is a long long
  • 2.0f is a float
  • 2.0 is a double
  • '\2' is a char.

But how would I write a short literal? I tried 2S but that gives a compiler warning.

C++ Solutions


Solution 1 - C++

((short)2)

Yeah, it's not strictly a short literal, more of a casted-int, but the behaviour is the same and I think there isn't a direct way of doing it.

> That's what I've been doing because I couldn't find anything about it. I would guess that the compiler would be smart enough to compile this as if it's a short literal (i.e. it wouldn't actually allocate an int and then cast it every time).

The following illustrates how much you should worry about this:

a = 2L;
b = 2.0;
c = (short)2;
d = '\2';

Compile -> disassemble ->

movl	$2, _a
movl	$2, _b
movl	$2, _c
movl	$2, _d

Solution 2 - C++

C++11 gives you pretty close to what you want. (Search for "user-defined literals" to learn more.)

#include <cstdint>

inline std::uint16_t operator "" _u(unsigned long long value)
{
    return static_cast<std::uint16_t>(value);
}

void func(std::uint32_t value); // 1
void func(std::uint16_t value); // 2

func(0x1234U); // calls 1
func(0x1234_u); // calls 2

// also
inline std::int16_t operator "" _s(unsigned long long value)
{
    return static_cast<std::int16_t>(value);
}

Solution 3 - C++

Even the writers of the C99 standard got caught out by this. This is a snippet from Danny Smith's public domain stdint.h implementation:

/* 7.18.4.1  Macros for minimum-width integer constants

    Accoding to Douglas Gwyn <gwyn@arl.mil>:
	"This spec was changed in ISO/IEC 9899:1999 TC1; in ISO/IEC
	9899:1999 as initially published, the expansion was required
	to be an integer constant of precisely matching type, which
	is impossible to accomplish for the shorter types on most
	platforms, because C99 provides no standard way to designate
	an integer constant with width less than that of type int.
	TC1 changed this to require just an integer constant
	*expression* with *promoted* type."
*/

Solution 4 - C++

If you use Microsoft Visual C++, there are literal suffixes available for every integer type:

auto var1 = 10i8;  // char
auto var2 = 10ui8; // unsigned char

auto var3 = 10i16;  // short
auto var4 = 10ui16; // unsigned short

auto var5 = 10i32;  // int
auto var6 = 10ui32; // unsigned int

auto var7 = 10i64;  // long long
auto var8 = 10ui64; // unsigned long long

Note that these are a non-standard extension and aren't portable. In fact, I couldn't even locate any info on these suffixes on MSDN.

Solution 5 - C++

You can also use pseudo constructor syntax.

short(2)

I find it more readable than casting.

Solution 6 - C++

One possibility is to use C++11 "list initialization" for this purpose, e.g.:

short{42};

The advantage of this solution (compared to a cast as in the currently accepted answer) is that it does not allow narrowing conversions:

auto number1 = short(100000); // Oops: Stores -31072, you may get a warning
auto number2 = short{100000}; // Compiler error. Value too large for type short

See https://en.cppreference.com/w/cpp/language/list_initialization#Narrowing_conversions for prohibited narrowing conversions with list-init

Solution 7 - C++

As far as I know, you don't, there's no such suffix. Most compilers will warn if an integer literal is too large to fit in whatever variable you're trying to store it in, though.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKipView Question on Stackoverflow
Solution 1 - C++Mike FView Answer on Stackoverflow
Solution 2 - C++Ken SmithView Answer on Stackoverflow
Solution 3 - C++Michael BurrView Answer on Stackoverflow
Solution 4 - C++Alexander RevoView Answer on Stackoverflow
Solution 5 - C++jimvonmoonView Answer on Stackoverflow
Solution 6 - C++P. SaladinView Answer on Stackoverflow
Solution 7 - C++unwindView Answer on Stackoverflow