Template Class in C ++

What is the function of the following C ++ template class? I am after line after line of annotation:

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

How is the Java toString() method?

+5
c ++ tostring templates
Aug 18 '10 at 13:39
source share
7 answers

Basically, it takes any object that has an operator <<defined for output to a stream, and converts it to a string. If you wish, if you pass the bool varaible address, it will set this based on whether the conversion has been achieved.

The advantage of this function is that after you define it, once you define the <operator for the new class you are writing, you can also get the toString () method for it.

  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 - declares ostringstream - an output stream that writes to a string
  • B - Write your object to this stream using its operator <<
  • C - Set * ok boolean for success / failure
  • D - convert the stream to a standard string and return it.
+7
Aug 18 '10 at 13:54
source share

This template function accepts a value of any type and a pointer to bool . It tries to use std::ostringstream to convert a value to std::string using the formatting conversions provided by the output streams. If the bool -pointer parameter is not equal to zero, the function records whether the stream operation performed the value with the specified pointer.

Thus, you can write:

 std::string s = toString(123); 

But you can also write:

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

That is, the function allows you to check whether the conversion was successful.

+3
Aug 18 '10 at 13:57
source share

Yes and no. It works on any object for which the << operator has been defined using ostream. This or any object for which ostringstream has an overloaded method to process.

Any object in question in a function has the following meanings:

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

or it falls into one standard overload. Here is a list of overloaded functions found in `ostream ', taken from here :

ostream & Operator <<(bool & val);

ostream & Operator <<(short & val);

ostream & Operator <<(unsigned short & val);

ostream & Operator <<(int & val);

ostream & Operator <<(unsigned int & val);

ostream & Operator <<(long & val);

ostream & Operator <<(unsigned long & val);

ostream & Operator <<(float & val);

ostream & Operator <<(double & val);

ostream & Operator <<(long double & val);

ostream & Operator <<(const void * val);

ostream & Operator <<(streambuf * sb);

ostream & Operator <<(ostream & (* pf) (ostream &));

ostream & Operator <<(ios & (* pf) (ios &));

ostream & Operator <<(ios_base & (* pf) (ios_base &));

*** The following functions are not members, but GLOBAL functions:

ostream & Operator <<(ostream & out, char c);

ostream & Operator <<(ostream & out, signed char c);

ostream & Operator <<(ostream & out, unsigned char c);

ostream & Operator <<(ostream & out, const char * s);

ostream & Operator <<(ostream & out, const signed char * s);

ostream & Operator <<(ostream & out, const unsigned char * s);

+1
Aug 18 '10 at 13:41
source share

This is not a template class. This is a boilerplate function / method. Indeed, he is trying to put the argument "t" into the stream. An operation may fail if the output stream (ostringstream) does not support input processing of type "T" (the operator "<" does not know how to handle objects of type T).

+1
Aug 18 2018-10-18
source share

This is actually just a template function, not a class. It provides simplified semantics for string conversion for any type that can work with ostringstream (all numeric types will work and additional custom conversions can be defined). The function places the value in the stream and then returns a string representation of the stream.

+1
Aug 18 '10 at 13:47
source share

Firstly, this is not a class, but just a function. Here's the annotated version:

 // 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 (eg what would // have appeared on the terminal if stream were instead std::cout) return stream.str(); } 
0
Aug 18 2018-10-18
source share

@Luna, as mentioned in Wheaties, is that the template parameter type T in your function template<class T> string toString(const T& t, bool *ok = NULL) { must either be part of the list of data types or type T must implement the ostream <statement. Otherwise, the function will not work.

0
Aug 18 '10 at 13:57
source share



All Articles