개요
- 인자로 전달된 n개의 타입들이 변환할 수 있는 공통 타입으로 변환
예제
- 코드
#include <iostream>
#include <string>
#include <type_traits>
#include <typeinfo>
using namespace std;
class Base {};
class Derived : public Base {};
template <typename T1, typename T2>
auto plus_t(T1 t1, T2 t2) -> typename common_type<T1, T2>::type {
return t1 + t2;
}
int main() {
cout << typeid(common_type<int, float, double>::type).name() << endl;
cout << typeid(common_type<char, int>::type).name() << endl;
cout << typeid(common_type<char *, string>::type).name() << endl;
cout << typeid(common_type<Base, Derived>::type).name() << endl;
cout << typeid(common_type<Base *, Derived *>::type).name() << endl;
cout << plus_t(1, 1) << endl;
cout << plus_t(1, 1.1) << endl;
cout << plus_t("a", string("a")) << endl;
return 0;
}
- 실행 결과
d
i
NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
4Base
P4Base
2
2.1
aa