example5_void_as_template_parameter.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <type_traits>

template<typename T>
struct Helper {
    static void print() {
        std::cout << "Non-void type" << std::endl;
    }
};

template<>
struct Helper<void> {
    static void print() {
        std::cout << "Void type" << std::endl;
    }
};

template<typename T>
void process() {
    Helper<T>::print();
}

// Overload for void
void process() {
    Helper<void>::print();
}

int main() {
    process<int>();
    process<void>();
    process();  // Calls the non-template overload

    return 0;
}
Back to void