c++ - 为什么 clang 让我在 C++03 模式下通过非常量引用获取临时值?

标签 c++ clang

Inspired by my observation in a previous question ,我决定做一个小测试:

#include <iostream>
#include <sstream>

int main()
{
  char c = 'A';
  std::stringstream ss("B");

  // I know this is bad mojo; that's why I'm testing it
  ss >> char(c);

  std::cout << c << std::endl;
}

我的编译器版本:

Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix

用C++03模式编译clang,编译运行正常:

$ clang++ -Wall -pedantic -std=c++03 test.cpp 
test.cpp:9:6: warning: expression result unused [-Wunused-value]
  ss >> char(c);
  ~~ ^  ~~~~~~~
1 warning generated.
$ ./a.out 
A

它打印出 A ,这很好,因为这段代码甚至不应该编译。切换到 C++11 模式,编译时正确出错(error: invalid operands to binary expression ('std::stringstream' (aka 'basic_stringstream<char>') and 'int'))。

是的,在 C++03 模式下它确实会发出警告,但这不是我期望的警告(我期望某种“通过引用获取临时”警告/错误,或者可能是警告/错误说不operator>> 接受一个 char 参数)。

我的问题是:为什么代码在 C++03 模式下成功编译?它是否使用了一些替代的 operator>> 重载?避免通过引用获取临时文件?是不是太松懈了,让我临时引用一下?我很困惑为什么 clang 完全接受这段代码; GCC 4.9 在 C++03/11 模式下正确出错,并出现更相关的错误 ( error: no match for 'operator>>' (operand types are 'std::stringstream {aka std::basic_stringstream<char>}' and 'char') )。 Clang 在 C++03 模式下的行为让我感到困惑。

最佳答案

我认为它调用了std::stringstreamoperator bool(),即代码被解释为

bool(ss) >> char(c);

这当然是一个有效的声明,但没有任何效果。到 bool 的隐式转换返回 !fail() 并且允许这样的代码

while (ss >> foo)
  ...

标准允许转换为另一种类型,该类型转换为 bool 而不是普通的 bool(例如 void*),正是为了避免这类问题。显然,您的实现没有利用这种自由。

关于c++ - 为什么 clang 让我在 C++03 模式下通过非常量引用获取临时值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25824357/

相关文章:

c++ - 该程序使用 Visual Studio 编译,但 clang 和 gcc 无法编译

gcc - clang 是否有相当于 GCC 的 -mno-vzeroupper 标志?

c++ - 使用来自不同线程的 opengl-command

c++ - 前缀/后缀增量运算符

c++ - 类声明后的初始化 C++

c++ - Lambda、局部类型和全局命名空间

c++ - 将 libssh 导入 Qt

c++ - 是否可以将 QColor 作为键存储在 QMap 中

c++ - 在 CodeLite (Windows) 中将编译器从 MinGW32 更改为 clang 会导致编译错误

clang 没有停留在#include "/dev/whatever"