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(); } 模板字符串到字符串(常量T&T,bool*ok=NULL){ ostringstream; 流

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(); } 模板字符串到字符串(常量T&T,bool*ok=NULL){ ostringstream; 流,c++,templates,tostring,C++,Templates,Tostring,是和否。它适用于运算符不是模板类的任何对象。它是模板函数/方法。 事实上,它确实试图将参数“t”放入流中。 如果输出流(ostringstream)失败,则操作可能无法成功 不支持处理类型“T”的输入(“这实际上只是一个模板函数而不是一个类。它提供了简化的语义,用于将任何可以使用ostringstream的类型转换为字符串(所有数字类型都可以工作,并且可以定义其他自定义转换)。函数将值放入流中,然后返回流的字符串表示形式。基本上,它将首先接受任何具有运算符的对象,这不是一个类,它只是一个函数。下

是和否。它适用于运算符
不是模板类的任何对象。它是模板函数/方法。
事实上,它确实试图将参数“t”放入流中。
如果输出流(ostringstream)失败,则操作可能无法成功

不支持处理类型“T”的输入(“这实际上只是一个模板函数而不是一个类。它提供了简化的语义,用于将任何可以使用
ostringstream
的类型转换为字符串(所有数字类型都可以工作,并且可以定义其他自定义转换)。函数将值放入流中,然后返回流的字符串表示形式。

基本上,它将首先接受任何具有运算符的对象,这不是一个类,它只是一个函数。下面是一个带注释的版本:

// This function accepts two parameters, one of which is a constant reference
// to any arbitrary type (the arbitrary type is referred to as T), and the 
// other is an optional pointer to boolean, which is NULL if left unspecified.

template<class T> string toString(const T& t, bool *ok = NULL) {

         // This instantiates an output string stream, which is a class provided
         // by the STL which works very much like std::cout except that the output
         // is stored in a string instead of sent to standard output.

         ostringstream stream;

         // This inserts the passed-in variable t into the stream. This requires
         // that a function operator<<(ostream&, const T&) exists for the 
         // particular type T that is the type of t. If it does not exist for some
         // T that this function is called on, there will be a compile error. This 
         // operator overload is provided by default for built-in types and some STL
         // types (such as std::string). The implementation of this function for any
         // given type T is responsible for doing whatever needs to be done to 
         // represent the value of t as a character stream. This is exactly what
         // happens when you write std::cout << t, except the result is sent to
         // a string inside the stringstream instead of to the console.

         stream << t;

         // If the user passed in a pointer-to-boolean, then check if the previous
         // line caused an error, and set the boolean to false if there was an error,
         // or true otherwise. An error might occur if the value in t can't be
         // represented as a string for whatever reason.

         if(ok != NULL) *ok = stream.fail() == false;

         // This returns the string that was created by the stream (e.g. what would
         // have appeared on the terminal if stream were instead std::cout)

         return stream.str();
}
//此函数接受两个参数,其中一个是常量引用
//任何任意类型(任意类型称为T),并且
//另一个是指向布尔值的可选指针,如果未指定,则为NULL。
模板字符串到字符串(常量T&T,bool*ok=NULL){
//这将实例化一个输出字符串流,它是提供的类
//通过STL,除了输出
//存储在字符串中,而不是发送到标准输出。
ostringstream;
//这会将传入的变量t插入流中

//这是一个函数运算符@Luna,Wheaties提到的是函数
模板字符串中模板参数T的类型为字符串(const T&T,bool*ok=NULL){
应该是数据类型列表的一部分,或者类型T应该实现ostream的此模板化函数接受任何类型的值和指向
bool
的指针。它尝试使用
std::ostringstream
使用输出提供的格式转换将值转换为
std::string
如果
bool
-pointer参数为非空,则函数会将流操作是否成功写入该指针处的值

因此,可以写:

std::string s = toString(123);
bool succeeded;
std::string s = toString(something, &succeeded);
if (succeeded) { ... }
但也可以写:

std::string s = toString(123);
bool succeeded;
std::string s = toString(something, &succeeded);
if (succeeded) { ... }

也就是说,该函数允许您检查转换是否成功。

如果流不知道如何处理
t
,编译将不会成功-但是如果
操作您是绝对正确的。我应该在回答时更加小心。