What if I have a generic class with type T and I have a function which returns with T. and I want my function to return a specific string if the typeid(T) == typedef(string)?
template<class T>
class Class1
{
public:
Class1();
~Class1();
T func();
};
template <class T>
T Class1<T>::func()
{
string d = "T = string";
if (typeid(string) == typeid(T))
return (T)d; <-- here I got the problem
}
Here's an example of explicit specialization of a member function of a class template:
#include <iostream>
#include <string>
template<class T>
class Class1
{
public:
Class1() {}
~Class1() {}
T func();
};
template <class T>
T Class1<T>::func()
{
std::cout << "non-specialized version\n";
return T();
}
template<>
std::string Class1<std::string>::func()
{
std::cout << "string version\n";
return "woot";
}
int main()
{
Class1<int>().func();
Class1<std::string>().func();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.