Should reading negative into unsigned fail via std::cin (gcc, clang disagree)?

前端 未结 3 1895
别那么骄傲
别那么骄傲 2020-12-30 20:29

For example,

#include 

int main() {
  unsigned n{};
  std::cin >> n;
  std::cout << n << \' \' << (bool)std::cin <         


        
3条回答
  •  囚心锁ツ
    2020-12-30 21:14

    The intended semantics of your std::cin >> n command are described here (as, apparently, std::num_get::get() is called for this operation). There have been some semantics changes in this function, specifically w.r.t. the choice of whether to place 0 or not, in C++11 and then again in C++17.

    I'm not entirely sure, but I believe these differences may account for the different behavior you're seeing.

提交回复
热议问题