我如何在C ++中写一个短文字? [英] How do I write a short literal in C++?
问题描述
非常基本的问题:我如何在C ++中写一个 short
文字?
Very basic question: how do I write a short
literal in C++?
-
2
是int
-
2U
是unsigned int
-
2L
是长
-
2LL
是长长
-
2.0f
是float
-
2.0
c> double -
'\2'
code>。
2
is anint
2U
is anunsigned int
2L
is along
2LL
is along long
2.0f
is afloat
2.0
is adouble
'\2'
is achar
.
但是我如何写一个 short
?我尝试了 2S
但是给出了编译器警告。
But how would I write a short
literal? I tried 2S
but that gives a compiler warning.
推荐答案
((short)2)
,更多的是casted-int,但是行为是一样的,我认为没有直接的方法。
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.
这是我一直在做,因为我找不到任何东西。我猜想编译器会聪明地编译这个,就像它是一个简短的字面量(即,它不会实际分配一个int,然后投射它每次)。
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 - >
Compile -> disassemble ->
movl $2, _a
movl $2, _b
movl $2, _c
movl $2, _d
这篇关于我如何在C ++中写一个短文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!