[英]A template class in 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()
方法?
基本上,它會將任何具有operator <<的輸出對象定義為流,並將其轉換為字符串。 (可選)如果您傳遞bool變量的地址,它將根據轉換是否成功來設置。
這個函數的優點是,一旦定義了,只要你為你編寫的新類定義了operator <<,你也可以立即得到一個toString()方法。
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
}
此模板化函數接受任何類型的值和指向bool
的指針。 它嘗試使用std::ostringstream
將值轉換為std::string
使用輸出流提供的格式轉換。 如果bool
-pointer參數為非null,則該函數會寫入流操作是否成功到該指針處的值。
因此可以寫:
std::string s = toString(123);
但它也可以寫:
bool succeeded;
std::string s = toString(something, &succeeded);
if (succeeded) { ... }
也就是說,該功能允許您檢查轉換是否成功。
是的,不是。 它適用於運算符<<
已使用ostream定義的任何對象。 那個或任何ostringstream
有重載方法處理的對象。
您傳遞給函數的問題對象具有以下定義:
ostream& operator <<(ostream &os, MyObj &obj);
或者它屬於一個標准的重載。 下面是取自`ostream的”內發現的重載函數列表, 在這里 :
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&));
***以下函數不是成員,而是GLOBAL函數:
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);
它不是模板類。 這是一個模板功能/方法。 事實上它確實試圖將參數“t”放入流中。 如果輸出流(ostringstream)不支持處理類型為“T”的輸入(“<<”運算符不知道如何處理類型為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();
}
@Luna,Wheaties提到的是函數template<class T> string toString(const T& t, bool *ok = NULL) {
中的模板參數T的類型template<class T> string toString(const T& t, bool *ok = NULL) {
應該是數據類型列表或類型T的一部分應該實現ostream的<<運算符。 否則該功能將失敗。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.