Templates and Header Files

Template Specialization in Separate Files

How do I handle template specialization when using separate implementation files?

Illustration representing computer hardware

Handling template specialization with separate implementation files can be tricky, but it's doable with the right approach. Let's walk through the process step by step.

Declaration in Header File

First, declare both the primary template and its specializations in the header file:

// MyTemplate.h
#pragma once

template <typename T>
class MyTemplate {
public:
    void foo();
};

// Declare specialization
template <>
class MyTemplate<int> {
public:
    void foo();
};

Implementation in Separate File

Next, implement both the primary template and specializations in a separate .cpp file:

// MyTemplate.cpp
#include "MyTemplate.h"
#include <iostream>

template <typename T>
void MyTemplate<T>::foo() {
    std::cout << "General template\n";
}

// Implement specialization
void MyTemplate<int>::foo() {
    std::cout << "Specialized for int\n";
}

// Explicit instantiation
template class MyTemplate<double>;

Note the explicit instantiation at the end. This is crucial for making the template available to other translation units.

Usage

Now you can use your template in other files:

// main.cpp
#include "MyTemplate.h"

int main() {
  MyTemplate<double> d;
  d.foo();  // Outputs: "General template"

  MyTemplate<int> i;
  i.foo();  // Outputs: "Specialized for int"
}
General template
Specialized for int

Potential Pitfalls

  1. Forgetting explicit instantiation can lead to linker errors.
  2. If you add new specializations, you need to update both the header and implementation files.

Best Practices

  • Keep all specializations in the same header and implementation files as the primary template for better organization.
  • Use explicit instantiation for commonly used types to reduce compilation times in large projects.
  • Consider using export declarations (if supported by your compiler) for more complex scenarios.

Remember, while this approach works, it's often simpler to keep template definitions in header files, especially for smaller projects. Only move to separate implementation files if you have a specific need, such as reducing compilation times in large projects.

This Question is from the Lesson:

Templates and Header Files

Learn how to separate class templates into declarations and definitions while avoiding common linker errors

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

This Question is from the Lesson:

Templates and Header Files

Learn how to separate class templates into declarations and definitions while avoiding common linker errors

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