【问题标题】:A template class in C++C++ 中的模板类
【发布时间】:2011-03-31 13:45:01
【问题描述】:

下面的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() 方法?

【问题讨论】:

    标签: c++ templates tostring


    【解决方案1】:

    基本上,它会将任何定义了 operator

    这个函数的优点是,一旦定义了 operator

     template<class T> 
     string toString(const T& t, bool *ok = NULL) 
     { 
             ostringstream stream;     // line A
             stream << t;              // line B
             if(ok != NULL) 
                   *ok = (stream.fail() == false);  // line C
             return stream.str();      // Line D
    } 
    
    • A - 声明 ostringstream - 写入字符串的输出流
    • B - 使用它的操作符将您的对象写入该流中
    • C - 将 *ok 布尔值设置为成功/失败
    • D - 将流转换为标准字符串并返回。

    【讨论】:

      【解决方案2】:

      这个模板化的函数接受任何类型的值和一个指向bool的指针。它尝试使用std::ostringstream 使用输出流提供的格式转换将值转换为std::string。如果bool-pointer 参数不为空,则函数将流操作是否成功写入该指针处的值。

      因此可以这样写:

      std::string s = toString(123);
      

      但也可以这样写:

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

      即该功能允许您检查转换是否成功。

      【讨论】:

        【解决方案3】:

        它不是一个模板类。这是一个模板函数/方法。 事实上,它确实试图将参数“t”放入流中。 如果输出流(ostringstream),操作可能不会成功 不支持处理“T”类型的输入(“

        【讨论】:

        • 如果流不知道如何处理T编译将不会成功。 - 但是,如果为T 实现了operator&lt;&lt;,则实现可能会选择在流中设置一个失败位,以表明转换应在运行时被视为失败。对于内置类型,我不确定从流的角度来看转换为字符串是否会失败(可能的内存不足情况可能会引发异常?)。
        • 你是绝对正确的。我应该更加谨慎地表达我的答案。
        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          是和不是。它适用于已使用 ostream 定义了运算符 &lt;&lt; 的任何对象。 ostringstream 具有要处理的重载方法的那个或任何对象。

          您传递给函数的相关对象具有以下定义:

          ostream& operator <<(ostream &os, MyObj &obj);
          

          或者它属于标准重载之一。以下是从here 获取的“ostream”中的重载函数列表:

          ostream& 运算符

          ostream& 运算符

          ostream& 运算符

          ostream& 运算符

          ostream& 运算符

          ostream& 运算符

          ostream& 运算符

          ostream& 运算符

          ostream& 运算符

          ostream& 运算符

          ostream& 运算符

          ostream& 运算符

          ostream& 运算符

          ostream& 运算符

          ostream& 运算符

          *** 以下函数不是成员,而是 GLOBAL 函数:

          ostream& 运算符

          ostream& 运算符

          ostream& 运算符

          ostream& 运算符

          ostream& 运算符

          ostream& 运算符

          【讨论】:

            【解决方案6】:

            首先,这不是一个类,它只是一个函数。这是一个带注释的版本:

            // 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();
            }
            

            【讨论】:

              【解决方案7】:

              @Luna,Wheaties 提到的是函数template&lt;class T&gt; string toString(const T&amp; t, bool *ok = NULL) { 中模板参数 T 的类型应该是数据类型列表的一部分,或者类型 T 应该实现 ostream 的

              【讨论】:

                猜你喜欢
                • 2010-11-08
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-11-08
                相关资源
                最近更新 更多