如何在构造函数中应用算术类型提升

时间:2019-06-07 23:09:49

标签: templates constructor c++17 template-deduction type-promotion

假设地,如果我正在创建一个点类,并且希望它根据参数推导类型,我希望它可以将点类提升为最高参数。例如:

template <class dtype>
class Point;

...

auto x = Point(1, 1.0); // Point<double> specialized
auto y = Point(1.0, 1); // Point<double> specialized

我不确定如何在构造函数中实现此目标。我已经能够从调用显式专用构造函数的函数中推断出类型,而不是从构造函数本身中推断出类型。

到目前为止,这是我的尝试:

#include <type_traits>

template <typename... Ts>
struct promoted_type_wrap;

template <typename T>
struct promoted_type_wrap<T> {
  using type = T;
};

template <typename T, typename U, typename... Ts>
struct promoted_type_wrap<T, U, Ts...> {
  using type = typename promoted_type_wrap<typename std::conditional<
    (sizeof(U) <= sizeof(T)), T, U >::type, Ts... >::type;
};

template <typename... Ts>
using promoted_type = typename promoted_type_wrap<Ts...>::type;

template <typename T>
using same_type = typename promoted_type_wrap<T>::type;

template <class dtype>
class Point {
protected:
  dtype x, y;

public:
  constexpr Point(const dtype x, const same_type<dtype> y)
    : x(x), y(y) {
  }
};

template <class dtype, class etype>
constexpr auto make_Point(const dtype x, const etype y) {
  return Point<promoted_type<dtype, etype>>(x, y);
}

void test() {
  constexpr auto x = make_Point(1, 2.0); // Point<double> specialized
  constexpr auto y = make_Point(1.0, 2); // Point<double> specialized
  constexpr auto z = Point(1, 2.0); // Point<int> specialized
  constexpr auto w = Point(1.0, 2); // Point<double> specialized
}

为什么Point(1, 2.0)被专门化为Point<int>是有道理的,因为第一个参数是int,这会在构造函数中将第二个参数强制设为int;但是,我不确定如何重新构造构造函数,使其表现得像伪构造函数工厂一样。

1 个答案:

答案 0 :(得分:2)

  

但是,我不确定如何重新构造构造函数,使其表现得像伪构造函数工厂一样。

不是构造函数:您必须编写自定义推导指南。

以下内容

template <typename T1, typename T2>
Point(T1, T2) -> Point<promoted_type<T1, T2>>;

以下是完整的编译示例

#include <type_traits>

template <typename... Ts>
struct promoted_type_wrap;

template <typename T>
struct promoted_type_wrap<T>
 { using type = T; };

template <typename T, typename U, typename... Ts>
struct promoted_type_wrap<T, U, Ts...>
 { using type = typename promoted_type_wrap<std::conditional_t<
      (sizeof(U) <= sizeof(T)), T, U >, Ts... >::type; };

template <typename... Ts>
using promoted_type = typename promoted_type_wrap<Ts...>::type;

template <typename dtype>
class Point
 {
   protected:
      dtype x, y;

   public:
      template <typename T1, typename T2>
      constexpr Point (T1 const & a, T2 const & b) : x(a), y(b)
       { }
 };

template <typename T1, typename T2>
Point(T1, T2) -> Point<promoted_type<T1, T2>>;

int main ()
 {
   constexpr auto z = Point(1, 2.0); // now Point<double>
   constexpr auto w = Point(1.0, 2); // again Point<double> 

   static_assert( std::is_same_v<decltype(z), Point<double> const> );
   static_assert( std::is_same_v<decltype(w), Point<double> const> );
 }

非主题:我认为不是像

那样根据类型的大小选择“升级类型”是一个好主意
template <typename T, typename U, typename... Ts>
struct promoted_type_wrap<T, U, Ts...>
 { using type = typename promoted_type_wrap<std::conditional_t<
      (sizeof(U) <= sizeof(T)), T, U >, Ts... >::type; };

即使忽略了其他问题,当您使用相同大小的不同类型时,所选类型也是第一个。

例如,在我的平台上,g ++和clang ++都具有sizeof(long) == sizeof(float),所以我们得到了

constexpr auto z = Point(1l, 2.0); // <-- deduced as Point<long>
constexpr auto w = Point(1.0, 2l); // <-- deduced as Point<double>

static_assert( std::is_same_v<decltype(z), Point<long> const> );
static_assert( std::is_same_v<decltype(w), Point<double> const> );

我建议使用某些东西来独立于类型的顺序选择“首选类型”。

在我看来,您应该按以下方式使用std::common_type

#include <type_traits>

template <typename dtype>
class Point
 {
   protected:
      dtype x, y;

   public:
      template <typename T1, typename T2>
      constexpr Point (T1 const & a, T2 const & b) : x(a), y(b)
       { }
 };

template <typename T1, typename T2>
Point(T1, T2) -> Point<std::common_type_t<T1, T2>>;

int main ()
 {
   constexpr auto z = Point(1l, 2.0); // <-- deduced as Point<double>
   constexpr auto w = Point(1.0, 2l); // <-- deduced as Point<double>

   static_assert( std::is_same_v<decltype(z), Point<double> const> );
   static_assert( std::is_same_v<decltype(w), Point<double> const> );
 }