I'm trying to write a c++ template method. Below is a sample code demonstrating what I want to do within the method.
template<class T>
void method(T value) {
// This string should change based on type T
char *str = "Int" or "Float" or .. ;
...
...
std::cout << value << " is of type " << str << std::cout;
}
Basically, the behavior (the string value in this example) of the method will change based on the type T. How could I do this with template?
You can specialize your template over different types. If you start with a base case:
template <class T>
void method(T value);
You can then declare different behavior for any specific value of T
:
template <>
void method<int>(int value) {
// special behavior
}
And so forth. But since only the input type of your function is changing, you really don't need templates in this case! You can just overload your function with different argument types:
void method(int T);
void method(float T);
void method(void* T);
EDIT: Using template specialization to get the name of a type and use it in another function template:
template <class T>
std::string type_to_string();
template <>
std::string type_to_string<int>() {
return "int";
}
template <>
std::string type_to_string<float>() {
return "float";
}
template <class T>
some_other_function(T value) {
std::cout << value << " is a " << type_to_string<T>() << std::endl;
}
Of course, you can still do this without templates:
std::string type_to_string(int) {
return "int";
}
some_other_function(int value) {
std::cout << value << " is a " << type_to_string(value) << std::endl;
}
If you had to do some complex type-level computation, I'd suggest using templates. But here, I think you can accomplish what you want rather nicely without them. Either way, the idiomatic way (with or without templates) is to split your function into its different natural pieces.
Off the top of my head a couple of ways:
Then you can say:
char *str = T.getTheAppropriateString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With