Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ template method, changing the behavior based on the template class

Tags:

c++

templates

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?

like image 339
Neo Hpcer Avatar asked Dec 03 '22 08:12

Neo Hpcer


2 Answers

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.

like image 128
Jonathan Sterling Avatar answered May 20 '23 05:05

Jonathan Sterling


Off the top of my head a couple of ways:

  1. Perhaps T could have a method that returns the appropriate string, and all your T's provide that method.

Then you can say:

char *str = T.getTheAppropriateString();

  1. RTTI and dynamic_cast. See http://en.wikipedia.org/wiki/Run-time_type_information.
like image 31
Eric M Avatar answered May 20 '23 07:05

Eric M