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

template<typename T>
void checkType() {
    if (std::is_same<T, int>::value) {
        std::cout << "T is an int" << std::endl;
    } else if (std::is_same<T, double>::value) {
        std::cout << "T is a double" << std::endl;
    } else if (std::is_same<T, std::string>::value) {
        std::cout << "T is a std::string" << std::endl;
    } else {
        std::cout << "T is some other type" << std::endl;
    }
}

int main() {
    checkType<int>();
    checkType<double>();
    checkType<std::string>();
    checkType<float>();

    return 0;
}
Back to std_is_same