signed int vs int-有没有办法在C ++中将它们区分开?

时间:2018-11-05 10:28:32

标签: c++ templates typetraits signed

是否有一个模板或方法可用于区分声明signed intint,如以下示例所示

signed int foo;
int bar;
static_assert(magic_signed<decltype(foo)>::value,"signed"); //PASS
static_assert(magic_signed<decltype(bar)>::value,"not signed"); //ASSERT

2 个答案:

答案 0 :(得分:5)

尽管signed intint是相同的,但我认为您可能想说的是如何区分unsigned intint。看一下std::is_signed。在c ++ 11之后可用。

https://en.cppreference.com/w/cpp/types/is_signed

还有一个std::is_unsigned

https://en.cppreference.com/w/cpp/types/is_unsigned

如果您实际上是要区分signed int和普通int,我认为您不能这样做,因为int被隐式理解为signed int。编译器,所以它们实际上是完全一样的。

也来看看这个。 Difference between int and signed int declaration

答案 1 :(得分:3)

intsigned intsigned是同一类型。

unsigned intunsigned是同一类型。