【发布时间】:2021-06-05 15:03:49
【问题描述】:
我以前用std::cout << str << std::endl显示字符串,认为隐式转换为std::string的对象也可以这样显示。
但是,我发现我错了。在我为 std::string 重载 operator << 之前,我不能 std::cout 隐式转换的 std::string。
以下代码演示了上述内容。
#include <stdio.h>
#include <iostream>
#include <string>
class X
{
public:
operator std::string() const {
return std::string("world");
}
};
#ifdef OVERLOAD_STREAM_OP
std::ostream& operator<<(std::ostream& os, const std::string& s) {
return os << s;
}
#endif
int main() {
std::string s = "hello";
std::cout << s << std::endl; // viable
printf("---\n");
X x;
std::cout << x << std::endl; // not viable
return 0;
}
看起来,在 STL 实现中,std::string 类型的重载 operator << 函数有点不同(但我真的不明白那些模板的东西):
template<typename _CharT, typename _Traits, typename _Allocator>
std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const basic_string<_CharT, _Traits, _Allocator>& __str)
{ return __os << __str._M_base(); }
我的问题:
-
STL 重载的
operator <<和我自己重载的operator<<std::string类型有什么区别? -
为什么我不能用
std::cout显示隐式转换的std::string对象x(编译错误)?
【问题讨论】:
-
那个
OVERLOAD_STREAM_OP位,你怎么认为这是合法的?您正在添加一个重载,但没有一个类型是您的。
标签: c++ templates operator-overloading overload-resolution