【发布时间】:2014-02-19 21:50:10
【问题描述】:
cout 是 ostream 类的对象,ostream 是 basic_ostream 的 typedef:
extern ostream cout;
typedef basic_ostream<char> ostream;
template <class charT, class traits = char_traits<charT> >
class basic_ostream;
但是这些类都没有operator<
所以我不明白为什么这段代码编译时没有任何错误:
std::cout < "aaa";
【问题讨论】:
-
en.cppreference.com/w/cpp/io/basic_ios/operator_bool - 从它的编译来看,它使用的是
void *,所以它触发了未定义的行为,因为指针不相关。 -
@omid 只是为了让您理解上面的评论,您使用的是“
-
@chris 我正在使用 c++11 。为什么它使用 void* ?
-
@omid,AFAIK,较新版本的 libc++ 进行了切换,但还没有其他任何东西。我可能是错的,但可以肯定的是你的图书馆没有。
-
顺便说一句,
std::ostream不是std::basic_ostream的对象,它是typedef的std::basic_ostream<char>,一个窄字符输出流。