Template Methods in Non-Template Classes

How do I create a template method within a non-template class?

Creating a template method within a non-template class is a powerful feature in C++ that allows you to have generic functionality within a specific class.

This can be particularly useful when you want to provide flexibility for a single method without making the entire class a template.

Here's an example of how to create a template method in a non-template class:

#include <iostream>
#include <list>
#include <vector>

class DataProcessor {
 public:
  // Non-template method
  void ProcessInt(int value) {
    std::cout << "Processing int: "
      << value << '\n';
  }

  // Template method
  template <typename T>
  void ProcessGeneric(const T& value) {
    std::cout << "Processing generic value: "
      << value << '\n';
  }

  // Template method with multiple parameters
  template <typename T, typename Container>
  void ProcessContainer(const Container& container) {
    std::cout << "Processing container:\n";
    for (const T& item : container) {
      std::cout << "  " << item << '\n';
    }
  }
};

int main() {
  DataProcessor processor;

  // Using non-template method
  processor.ProcessInt(42);

  // Using template methods
  processor.ProcessGeneric(3.14);
  processor.ProcessGeneric("Hello");

  std::vector<int> vec = {1, 2, 3, 4, 5};
  processor.ProcessContainer<int>(vec);

  std::list<std::string> list = {
    "apple", "banana", "cherry"
  };
  processor.ProcessContainer<std::string>(list);
}
Processing int: 42
Processing generic value: 3.14
Processing generic value: Hello
Processing container:
  1
  2
  3
  4
  5
Processing container:
  apple
  banana
  cherry

In this example, DataProcessor is a non-template class, but it contains template methods. Here's what's happening:

  1. ProcessInt is a regular, non-template method that works specifically with int.
  2. ProcessGeneric is a template method that can work with any type. The type T is deduced from the argument passed to the method.
  3. ProcessContainer is a more complex template method that takes two template parameters: the type of elements in the container (T) and the container type itself (Container).

When using template methods, you typically don't need to specify the template arguments explicitly - the compiler can deduce them from the function arguments.

However, in some cases (like with ProcessContainer), you might need to specify some of the template arguments explicitly if they can't be deduced.

Some key points to remember:

  • Template methods must be defined in the header file, not in a separate .cpp file. This is because the compiler needs to see the full definition to generate the code for each instantiation.
  • You can have both template and non-template methods in the same class.
  • Template methods can coexist with regular methods that have the same name, participating in overload resolution.
  • You can specialize template methods for specific types, similar to how you can specialize class templates.

Template methods in non-template classes provide a great way to add generic functionality to your classes without the need to make the entire class a template. This can lead to more flexible and reusable code.

Class Templates

Learn how templates can be used to create multiple classes from a single blueprint

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Partial Specialization of Class Templates
Is it possible to partially specialize a class template?
Compile-Time Polymorphism with Templates
Can I use templates to implement compile-time polymorphism?
Creating Templates with Variable Number of Parameters
How can I create a template that works with an arbitrary number of type parameters?
Default Values for Non-Type Template Parameters
Is it possible to have default values for non-type template parameters?
Constraining Template Arguments
How can I create a template that only accepts certain types of arguments?
Typename vs Class in Template Declarations
What's the difference between typename and class in template parameter declarations?
Creating Class Templates with Multiple Types
How can I create a class template that works with both primitive types and user-defined types?
Best Practices for Naming Template Parameters
What are the best practices for naming template parameters?
Specializing Class Templates
How can I specialize a class template for specific types?
Using Template Parameters in Constructors
Can I use template parameters in the constructor of a class template?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant