Reputation: 1513
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
}
Upvotes: 0
Views: 85
Reputation: 39101
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();
}
Upvotes: 6