自从C++ 11引入类型特征std::common_type
以来。 std::common_type
确定所有模板参数之间的通用类型。在C++ 14中,为了使使用std::common_type
类型特征的代码更短,还引入了帮助类型std::common_type_t
。用户定义类型的std :: common_type trait
std::common_type
是重载算术运算符特别有用,例如,
template<typename T1, typename T2>
std::common_type_t<T1, T2> operator+(T1 const &t1, T2 const &t2) {
return t1 + t2;
}
如果模板参数是内建类型,它工作正常(例如,int
,double
)。不过,我似乎不工作,如果我提供的模板参数给它的用户定义类型例如,
struct A {};
struct B {};
std::common_type_t<A, B> // doesn't work
Q:我怎样才能让std::common_type
与用户定义类型特质的工作?