gcc g++/stdlib++和clang++/libc++之间从流中阅读double的区别

pzfprimi  于 2023-10-19  发布在  其他
关注(0)|答案(1)|浏览(119)

最小的“失败”例子:

#include <assert.h>
#include <iostream>
#include <string>
#include <sstream>
int main(int argv, char** argc) {
  std::string line = "1.1", line2 = "1.1X";
  std::istringstream iss(line), iss2(line2);
  double x;
  assert(iss >> x);
  std::cout << x << "\n" << std::flush;
  assert(iss2 >> x);
  std::cout << x << "\n";
  return 0;
}

运行clang:

>clang++ -stdlib=libc++ reading_double.cc -o reading_double
>./reading_double 
1.1
reading_double: reading_double.cc:11: int main(int, char **): Assertion `iss2 >> x' failed.
Aborted (core dumped)
>clang --version
Ubuntu clang version 18.0.0 (++20231004042239+548d67a0393c-1~exp1~20231004042400.1224)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

使用GCC:

>g++ reading_double.cc -o reading_double
>./reading_double 
1.1
1.1
>g++ --version
g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

哪种行为才是正确的?(在我看来,libc++是错误的)

相关问题