ofstream或ostream类型如何将所有类型转换为字符串? [英] how does ofstream or ostream type cast all types to string?
问题描述
过去到ostream对象的任何系统定义的用户类型都将转换为字符串或char *?
any system defined user type past to ostream object is converted to a string or char* ?
like cout<<< 4<""Hello World";
like cout<<4<<"Hello World";
可以很好地工作,这是如何实现的?是<<每种类型的运算符重载?有没有一种方法可以仅通过一个通用的重载函数来实现?我的意思是我可以只使用一个带有一个参数的重载运算符方法(例如void *),然后在该方法内部决定如何将整数类型转换为char *
works perfectly fine, how is this achieved? is the << operator overloaded for each and every type? is there a way to achieve it through just one generic overloaded function? what i mean is can i have just one overloaded operator method with one parameter(like void*) and then decide inside that method how to typecast integer to char*
如果我重载了运算符<<使用模板即
Things worked partially if i overload operator << using Template i.e
class UIStream
{
private:
ofstream stream;
public:
UIStream();
~UIStream();
template <typename T>
UIStream& operator << (const T);
};
这可行
UIStream my_stream;
my_stream<<"bcd"<10;
但是当我这样做时它会给编译器带来错误
however it gives compiler error when i do this
my_stream <<endl;
错误C2678:二进制'<<' :找不到使用"UIStream"类型的左操作数(或没有可接受的转换)的运算符
error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'UIStream' (or there is no acceptable conversion)
std :: endl也不也是一种对象吗?
Is not std::endl a type of object too?
推荐答案
重新阅读您的问题后(由于对此此处),而是转发到内部的流中.
After re-reading your question (as a result of a comment in this answer) I have realized that what you want is not only conversions into string (my assumptions in the other answer here), but rather forwarding into the internal ofstream.
现在,您想要实现的目标并不简单,并且在大多数情况下可能过于矫kill过正.在我拥有的[make_string][3]
的实现中(转发到内部的ostringstream
),我不允许传递操纵器.如果用户想添加新行(我们在Linux下开发),则只需传递一个'\ n'字符即可.
Now, what you want to achieve is not simple, and may be overkill in most cases. In the implementation of [make_string][3]
that I have (that forwards to an internal ostringstream
), I don't allow for manipulators to be passed. If the user wants to add a new line (we develop under linux) they just pass a '\n' character.
您的问题是转发操纵器(std::hex
,std::endl
...).您的运算符<<被定义为采用类型T的常量实例,但是操纵器是函数指针,并且编译器无法将其与您的方法进行匹配.
Your problem is forwarding manipulators (std::hex
, std::endl
...). Your operator<< is defined as taking a constant instance of a type T, but manipulators are function pointers and the compiler is not able to match it against your methods.
操纵器是在std::basic_ostream
模板上运行的功能. basic_ostream
模板和ostream
类定义为:
Manipulators are functions that operate on the std::basic_ostream
template. The basic_ostream
template and ostream
class are defined as:
template <typename TChar, typename TTraits = char_traits<TChar> >
class basic_ostream;
typedef basic_ostream<char> ostream;
// or
// typedef basic_ostream<wchar_t> if using wide characters
然后可以传递给std :: ostream的可能的操纵器是:
Then the possible manipulators that can be passed to a std::ostream are:
typedef std::ostream& (*manip1)( std::ostream& );
typedef std::basic_ios< std::ostream::char_type, std::ostream::traits_type > ios_type;
typedef ios_type& (*manip2)( ios_type& );
typedef std::ios_base& (*manip3)( std::ios_base& );
如果要接受操纵器,则必须在类中提供该重载:
If you want to accept manipulators you must provide that overload in your class:
class mystream
{
//...
public:
template <typename T>
mystream& operator<<( T datum ) {
stream << datum;
return *this
}
// overload for manipulators
mystream& operator<<( manip1 fp ) {
stream << fp;
return *this;
}
mystream& operator<<( manip2 fp ) {
stream << fp;
return *this;
}
mystream& operator<<( manip3 fp ) {
stream << fp;
return *this;
}
};
特别是,endl的签名(可能是您唯一需要的签名)是:
In particular, the signature for endl (which may be the only one you require) is:
template <typename Char, typename Traits>
std::basic_ostream<Char,Traits>&
std::endl( std::basic_ostream<Char,Traits>& stream );
,因此它属于manip1
函数类型.其他诸如std::hex
属于不同类别(在这种情况下为manip3
)
so it falls under the manip1
type of functions. Others, like std::hex
fall under different categories (manip3
in this particular case)
这篇关于ofstream或ostream类型如何将所有类型转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!