Class Templates

Template Methods in Non-Template Classes

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

Illustration representing computer hardware

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.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved