I have the following problem:
template<class T>
void set(std::string path, const T data)
{
stringstream ss;
ss << data << std::endl;
write(path, ss.str();
}
template<class T>
void set(std::string path, const T data)
{
std::stringstream ss;
for(typename T::const_iterator it = data.begin(); it < data.end(); ++it)
{
ss << *it;
if(it < data.end() -1 )
ss << ", ";
}
ss << std::endl;
write(path, ss.str());
}
I get the following error:
error: ‘template<class T> void myclass::set(std::string, T)’ cannot be overloaded
error: with ‘template<class T> void myclass::set(std::string, T)’
Is there a way to differentiate between container types and other types in templates?
In C++03 you can do this with a little bit of SFINAE to selectively enable different versions of the function for different types:
I used boost for convenience here, but you could roll your own if boost isn’t an option or use C++11 instead.
is_podisn’t quite what you want really, you probably want anis_containertrait, but that’s not so trivial, you’ll need to make a trait of your own andis_podmakes a good approximation for how to use traits to selectively enable functions as simple answer.