Why does < instead of << in stream output still compile?

大兔子大兔子 提交于 2019-12-01 03:13:05
zneak

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

Which makes it clear that operator void* is the culprit.

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.

This is only valid before C++11.

You're basically doing: ((void *) std::cout) < ((char *) "test")

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!