example1_function_template_specialization.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <type_traits>

template <typename T>
void print_type(T value) {
    std::cout << "Generic template: " << value << std::endl;
}

template <>
void print_type<int>(int value) {
    std::cout << "Specialized template for int: " << value << std::endl;
}

int main() {
    print_type(3.14);  // Uses generic template
    print_type(42);    // Uses specialized template for int
    return 0;
}
Back to template_specialization