如何在 C++ 中编写一个简短的文字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/208433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:43:44  来源:igfitidea点击:

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

c++literals

提问by Kip

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

非常基本的问题:如何short在 C++ 中编写文字?

I know the following:

我知道以下几点:

  • 2is an int
  • 2Uis an unsigned int
  • 2Lis a long
  • 2LLis a long long
  • 2.0fis a float
  • 2.0is a double
  • '\2'is a char.
  • 2是一个 int
  • 2U是一个 unsigned int
  • 2L是一个 long
  • 2LL是一个 long long
  • 2.0f是一个 float
  • 2.0是一个 double
  • '\2'是一个char

But how would I write a shortliteral? I tried 2Sbut that gives a compiler warning.

但是我怎么写一个short文字呢?我试过了,2S但这会发出编译器警告。

采纳答案by Kip

((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).

这就是我一直在做的事情,因为我找不到任何关于它的信息。我猜编译器会足够聪明,可以把它编译成一个短文字(即它实际上不会分配一个 int 然后每次都强制转换)。

The following illustrates how much you should worry about this:

以下说明您应该为此担心多少:

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

Compile -> disassemble ->

编译->反汇编->

movl    , _a
movl    , _b
movl    , _c
movl    , _d

回答by Ken Smith

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

C++11 让你非常接近你想要的。(搜索“用户定义的文字”以了解更多信息。)

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

回答by Michael Burr

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

甚至 C99 标准的作者也被这件事抓住了。这是丹尼史密斯公共领域stdint.h实现的片段:

/* 7.18.4.1  Macros for minimum-width integer constants

    Accoding to Douglas Gwyn <[email protected]>:
    "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."
*/

回答by Alexander Revo

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

如果您使用 Microsoft Visual C++,则每种整数类型都有可用的文字后缀:

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 extensionand aren't portable. In fact, I couldn't even locate any info on these suffixes on MSDN.

请注意,这些是非标准扩展不可移植。事实上,我什至无法在 MSDN 上找到有关这些后缀的任何信息。

回答by jimvonmoon

You can also use pseudo constructor syntax.

您还可以使用伪构造函数语法。

short(2)

I find it more readable than casting.

我发现它比铸造更具可读性。

回答by unwind

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.

据我所知,你没有,没有这样的后缀。但是,如果整数文字太大而无法放入您尝试将其存储的任何变量中,大多数编译器都会发出警告。