example1_function_overloading.cpp

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

// Function for integral types
template <typename T>
typename std::enable_if<std::is_integral<T>::value, bool>::type
is_odd(T i) {
    return bool(i % 2);
}

// Function for floating-point types
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value, bool>::type
is_odd(T x) {
    return bool(int(x) % 2);
}

int main() {
    std::cout << "Is 5 odd? " << is_odd(5) << std::endl;
    std::cout << "Is 5.0 odd? " << is_odd(5.0) << std::endl;
}
Back to std_enable_if