Lets say I have a function
template <typename T>
T foo() {
// do stuff
return T(/* some value*/);
}
so I could call it for example like this:
int my_int = foo<int>();
std::string my_string = foo<std::string>();
Is there any way for me to create 2 separate polymorphic functions such that I could have a function that only gets called if T is std::string?
I tried doing this using the following code:
template <typename T>
T foo() {
if (std::is_same<std::string, T>::value) {
// do string specific stuff
}
return T(/* some value */);
}
but I would get compilation errors as the operations I wanted to do when T was std::string did not work when T was other types. Is there any way to make this polymorphic or should I just use a different function name like fooString()?
Aucun commentaire:
Enregistrer un commentaire