C++ 中的模板类
下面的C++模板类的作用是什么?我正在逐行注释:
template<class T> string toString(const T& t, bool *ok = NULL) {
ostringstream stream;
stream << t;
if(ok != NULL) *ok = stream.fail() == false;
return stream.str();
}
它像Java的toString()
方法吗?
What is the function of the following C++ template class? I'm after line by line annotations:
template<class T> string toString(const T& t, bool *ok = NULL) {
ostringstream stream;
stream << t;
if(ok != NULL) *ok = stream.fail() == false;
return stream.str();
}
Is it like Java's toString()
method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
基本上,它将采用任何具有运算符 << 的对象。定义用于输出到流,并将其转换为字符串。或者,如果您传递 bool 变量的地址,它将根据转换是否成功进行设置。
这个函数的优点是,一旦定义,只要定义了operator<<对于您编写的新类,您也立即获得一个 toString() 方法。
Basically, it will take any object which has an operator<< defined for output to a stream, and converts it to a string. Optionally, if you pass the address of a bool varaible, it will set that based on whether or not the conversion succeeeded.
The advantage of this function is that, once defined, as soon as you defined operator<< for a new class you write, you immedaitely also get a toString() method for it as well.
此模板化函数接受任何类型的值和指向
bool
的指针。它尝试使用std::ostringstream
通过输出流提供的格式转换将值转换为std::string
。如果 bool 指针参数非空,则该函数将流操作是否成功写入该指针处的值。因此可以这样写:
但也可以这样写:
即该函数允许您检查转换是否成功。
This templated function accepts a value of any type and a pointer to a
bool
. It attempts to use astd::ostringstream
to convert the value to astd::string
using the formatting conversions supplied by output streams. If thebool
-pointer parameter is non-null, the function writes whether the stream operation succeeded to the value at that pointer.Thus it's possible to write:
But it's also possible to write:
That is, the function allows you to check whether the conversion was successful.
它不是模板类。这是一个模板函数/方法。
事实上,它确实尝试将参数“t”放入流中。
如果输出流(ostringstream) 则操作可能不会成功
不支持处理“T”类型的输入(“<<”运算符不支持)
知道如何处理 T 类型的对象)。
It's not a template class. It's a template function/method.
And indeed it does try to put the argument "t" into the stream.
The operation might not succeed if the output stream(ostringstream)
does not support handling input of type "T"(the "<<" operator does not
know how to handle objects of type T).
这实际上只是一个模板函数而不是一个类。它提供了简化的语义,用于将任何可以与 ostringstream 配合使用的类型转换为字符串(所有数字类型都可以使用,并且可以定义其他自定义转换)。该函数将值放入流中,然后返回流的字符串表示形式。
This is actually just a template function not a class. It provides simplified semantics for converting to a string for any type that can work with an
ostringstream
(all numeric types will work and additional custom conversions can be defined). The function puts the value into the stream and then returns the string representation of the stream.是和不是。它适用于任何使用 ostream 定义了运算符
<<
的对象。ostringstream
有要处理的重载方法的该对象或任何对象。您传递给函数的相关对象具有以下定义:
或者它属于标准重载之一。以下是 `ostream' 中的重载函数列表,取自 这里:
*** 以下函数不是成员函数,而是全局函数:
Yes and no. It works on any object for which the operator
<<
has been defined with an ostream. That or any object for whichostringstream
has an overloaded method to handle.Either the object in question that you pass into the function has the following defined:
or it falls into one the standard overloads. Here is a list of the overloaded functions found within `ostream' taken from here:
*** the following functions are not members but GLOBAL functions:
首先,这不是一个类,它只是一个函数。这是一个带注释的版本:
First, this is not a class, it's just a function. Here's an annotated version:
@Luna,Wheaties 提到的是函数
template中模板参数 T 的类型string toString(const T& t, bool *ok = NULL) {
应该是数据类型列表的一部分,或者类型 T 应该实现 ostream 的 <<操作员。否则该功能将失败。@Luna, what Wheaties mentioned is that the type of the template parameter T in your function
template<class T> string toString(const T& t, bool *ok = NULL) {
should either be part of the list of datatypes or the type T should implement the ostream's << operator. Else the function would fail.