example3_using_with_SFINAE.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
#include <iostream>
#include <type_traits>

// Function for int type
template<typename T>
typename std::enable_if<std::is_same<T, int>::value, void>::type
printTypeInfo(T value) {
    std::cout << "This is an integer: " << value << std::endl;
}

// Function for double type
template<typename T>
typename std::enable_if<std::is_same<T, double>::value, void>::type
printTypeInfo(T value) {
    std::cout << "This is a double: " << value << std::endl;
}

// Function for other types
template<typename T>
typename std::enable_if<!std::is_same<T, int>::value && !std::is_same<T, double>::value, void>::type
printTypeInfo(T value) {
    std::cout << "This is neither an int nor a double" << std::endl;
}

int main() {
    printTypeInfo(42);
    printTypeInfo(3.14);
    printTypeInfo(std::string("Hello"));

    return 0;
}
Back to std_is_same