Log in

No account? Create an account
По делам сюда приплыл, а не за этим [entries|archive|friends|userinfo]
Anatoly Vorobey

[ website | Website ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

[Links:| English-language weblog ]

знаковый или беззнаковый? [фев. 22, 2018|12:50 pm]
Anatoly Vorobey

(эта запись может быть интересна программистам, знающим C/C++)

Во внутреннем форуме на работе кто-то устроил опрос, который просто не могу не украсть:

Опрос #2078582 unsigned short

"foo" is an "unsigned short". Is the expression "foo + foo" signed or unsigned?

implementation defined

Не смотрите в комментарии перед голосованием, там можно обсуждать правильный ответ.

From: cohenj
2018-02-22 06:34 pm

right answer

look at http://en.cppreference.com/w/cpp/language/typeid

operator typeid(expression)
returns object of class std::type_info
(which has member function name())

The type_info class is neither CopyConstructible nor CopyAssignable

typeid(foo+foo).name() is "unsigned short"
(Ответить) (Thread)
[User Picture]From: migmit.dreamwidth.org
2018-02-23 11:52 am

Re: right answer

(Ответить) (Parent) (Thread)
From: cohenj
2018-02-23 12:47 pm

Re: right answer

You are right: look at http://en.cppreference.com/w/cpp/language/implicit_conversion

Integral promotion
prvalues of small integral types (such as char) may be converted to prvalues of larger integral types (such as int). In particular, arithmetic operators do not accept types smaller than int as arguments, and integral promotions are automatically applied after lvalue-to-rvalue conversion, if applicable. This conversion always preserves the value.
(Ответить) (Parent) (Thread)