common_type函数 包含于<type_traits>当中。
作用是返回参数列表中的参数都可以转换成的类型(有些拗口)。
C++11中的用法为
syd::common_type<int,float,double>::type // 返回值为double #include <iostream> #include <type_traits> int main() { typedef std::common_type<int, float, double>::type test; std::cout << "test is a int " << std::is_same<int, test>::value<< std::endl; std::cout << "test is a float " << std::is_same<float, test>::value<< std::endl; std::cout << "test is a double " << std::is_same<double, test>::value << std::endl;; system("pause"); return 0; } std::common_type<char,std::string>::type // 报错,无法互相转换
在C++ 14中,我们可以用 std::common_type_t<>来代替。