在参数包中分发模板包装器

问题描述 投票:1回答:1

我有几个模板化类型,Egg<T>Chick<T>

template<typename T>
struct Egg{};

template<typename T>
struct Chick{};

LoudNest<Chicks...>中包含小鸡,QuietNest<Eggs...>中包含卵:

template <typename... Chicks>
struct LoudNest {
  std::tuple<Chicks...> chicks;
};

template <typename... Eggs>
struct QuietNest {
  std::tuple<Eggs...> eggs;

  // more here.
};

我想在hatch上使用一种QuietNest<Eggs...>方法来产生LoudNest。 LoudNest在QuietNest中的每个Chick<T>应该有一个Egg<T>。我有一个函数QuietNest<Eggs...>::hatch_impl,可以创建一个std::tuple<Chicks...>,其中Chick都具有正确的类型参数。也就是说,QuietNest<Egg<double>, Egg<string>, Egg<char>>::hatch_impl将返回std::tuple<Chick<double>, Chick<string>, Chick<char>>。我在尝试将其包装在LoudNest构造函数中时遇到了麻烦:

template <typename... Eggs>
struct QuietNest {
  std::tuple<Eggs...> eggs;

  auto hatch() const {
    // hatchlings is a std::tuple of chicks templated how I want.
    auto hatchlings = std::apply(
      [&](auto... args) { return hatch_impl(std::make_tuple(), args...); },
      eggs);
    // This line causes an error:
    return LoudNest{hatchlings};
    // error: cannot refer to class template 'LoudNest' without a template argument
  }

  // The rest of this all works, but is included in case you want to poke at it:

  // base case: only one parameter was passed—the tuple of hatched chicks.
  template<typename...Chicks>
  std::tuple<Chicks...> hatch_impl(std::tuple<Chicks...> chicks) {
    return chicks;
  }

  // recursive case: in addition to the tuple of hatched chicks,
  // at least one egg was passed (possibly more in the tail)
  template<typename...Chicks, typename T, typename...Unhatched>
  std::tuple<Chicks..., Chick<T>> hatch_impl(
    std::tuple<Chicks...> chicks,
    const Egg<T>& egg,
    Unhatched... tail
  ) const {
    Chick<T> babyBird = hatchOne(egg);
    return hatch_impl(
      std::tuple_cat(chicks, std::make_tuple(babyBird)),
      tail...);
  }

  template<T>
  Chick<T> hatchOne(Egg<T> egg) { return Chick<T>{}; }
};

我以为我需要制造一个“转换器”,该转换器接受参数包的鸡蛋,并用相应类型的雏鸡产生LoudNest。从将单个Egg<T>转换为Chick<T>开始,我有:

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

template< template <typename> class E, typename T>
struct AsChick<E<T>> {
  static_assert(std::is_same<E<T>, Egg<T>>::value, "Expected AsChick to be used with an Egg<T>");
  using type = Chick<T>;
};

我遇到困难的地方是当我尝试对参数包执行相同操作时:

template<typename... Eggs>
struct AsLoudNest1 {
    using type = LoudNest<
        (AsChick<Eggs>::type)...
        // I want this to expand Eggs to produce
        // AsChick<Eggs0>::type, AsChick<Eggs1>::type, AsChick<Eggs2>::type, ...
        // but it doesn't looks like that's a supported type of expansion
    >;
};
static_assert(std::is_same<
  AsLoudNest1<Egg<int>, Egg<double>>::type,
  LoudNest<Chick<int>, Chick<double>>
>::value, "Expected AsLoudNest1 to convert my Egg<T>s to Chick<T>s");

并尝试第二个:

template <
    class E, // does this need to be template<typename> class E?
    typename... Rest>
struct AsLoudNest2 {
    using type = LoudNest<
      // Pretty sure the beginning is right.
      AsChick<E>::type,

      // This line feels wrong, AsLoudNest2<...>::type is a concrete type, not a parameter pack
      AsLoudNest2<Rest...>::type...
    >;
};
// also, feels like I need a base case for AsLoudNest2?

我的实际问题与实现解释器有关,其类为FormalParameter<T>Egg<T>),ActualParameter<T>Chick<T>)等。但是,我想避免将“参数”一词用于在示例代码中,因为我们已经在不同意义上讨论了参数包。

此帖子中的代码:https://godbolt.org/z/XBIEhm

c++ templates c++14 variadic-templates
1个回答
1
投票

我通过一些更改就可以解决您的示例:https://godbolt.org/z/3VW68f

© www.soinside.com 2019 - 2024. All rights reserved.