为什么<而不是<在流输出仍然编译? [英] Why does < instead of << in stream output still compile?
问题描述
今天我在我的程序中做了一个小的打字错误,并且正在徘徊为什么我没有得到任何输出,虽然程序编译良好。基本上它减少到这:
Today I made a small typo in my program, and was wandering why I wasn't getting any output, although the program compiled fine. Basically it reduces to this:
#include <iostream>
int main()
{
std::cout < "test"; // no << but <
}
我完全不知道这里执行什么样的隐式转换,所以程序仍然编译(g ++ 4.9.2和甚至g ++ 5)。我只是意识到clang ++拒绝代码。是否有转换到 void *
执行(不能想到别的)?我记得看到这样的东西,但我认为这是在g ++ 5中解决,但似乎不是这样的情况。
I have absolutely no idea what kind of implicit conversion is performed here so the program still compiles (both g++4.9.2 and even g++5). I just realized that clang++ rejects the code. Is there a conversion to void*
being performed (cannot think of anything else)? I remember seeing something like this, but I thought it was addressed in g++5, but this doesn't seem to be the case.
编辑:我没有使用 -std = c ++ 11
所以代码在前C ++ 11中是有效的(由于转换为 void * of
ostream
)。当用 -std = c ++ 11
g ++ 5拒绝代码编译时,g ++ 4.9仍然接受它。
I was not compiling with -std=c++11
, so the code was valid in pre-C++11 (due to conversion to void*
of ostream
). When compiling with -std=c++11
g++5 rejects the code, g++4.9 still accepts it.
推荐答案
是的,编译器将 cout
转换为 void *
。如果你使用 -S
开关来获取代码的反汇编,你会看到这样的:
Yes, the compiler is converting cout
to a void*
. If you use the -S
switch to get the code's disassembly, you'll see something like this:
mov edi, OFFSET FLAT:std::cout+8
call std::basic_ios<char, std::char_traits<char> >::operator void*() const
cmp rax, OFFSET FLAT:.LC0
setb al
test al, al
这说明 operator void *
是罪魁祸首。
与Bill Lynch所说的相反,我可以在 -std = c ++ 11 goo.gl/RgweWSrel =nofollow>编译器资源管理器。但是,它似乎是一个实现缺陷,因为C ++ 11应该替换 operator void *
和 operator bool
basic_ios
。
Contrary to what Bill Lynch said, I'm able to reproduce it with —std=c++11
on Compiler Explorer. However, it does appear to be an implementation defect, since C++11 should have replaced operator void*
with operator bool
on basic_ios
.
这篇关于为什么<而不是<在流输出仍然编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!