用户定义类型的std::common_type特性

8
自从C++11引入了类型特征std::common_type。std::common_type确定其所有模板参数之间的公共类型。在C++14中,它的辅助类型std::common_type_t也被引入,以使使用std::common_type类型特征的代码更短。
std::common_type在重载的算术运算符中特别有用,例如:
template<typename T1, typename T2>
std::common_type_t<T1, T2> operator+(T1 const &t1, T2 const &t2) {
  return t1 + t2;
}

如果它的模板参数是内置类型(例如intdouble),那么它可以正常工作。但是,如果我将用户定义的类型作为模板参数提供给它,似乎无法正常工作,例如:

struct A {};
struct B {};

std::common_type_t<A, B> // doesn't work

: 我如何让std::common_type特性适用于用户自定义类型?

1个回答

15
根据草案标准N4582§20.13.2 Header synopsis [meta.type.synop](Emphasis Mine):

除非另有规定,否则程序为在本子条款中定义的任何模板添加特化的行为是未定义的。

因此,为type_traits添加特化可能会导致未定义的行为,除非标准中其他地方有特定类型特性的措辞超越上述显示的措辞。 幸运的是,在表格60-其他转换中:

enter image description here

这里是原文:

A program may specialize this trait if at least one template parameter in the specialization is a user-defined type. [ Note: Such specializations are needed when only explicit conversions are desired among the template arguments. — end note ]

这意味着,至少有一个用户定义的类型在模板参数中时,可以对该特性进行专门化。实际上,如果您查看§20.15.4.3 Specializations of common_type [time.traits.specializations],您会发现STL已经为用户定义的类型std::chrono::durationstd::chrono::time_point定义了std::common_type的专门化。因此,使common_type适用于用户定义的类型的正确方法是为这些特定类型提供专门化,例如:
struct A {};
struct B {};

namespace std {
  template<>
  struct common_type<A, B> {
    using type = A;
  };
} 

在上面的代码示例中,我们指定AB之间的公共类型是A

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接