【发布时间】:2020-04-27 01:53:27
【问题描述】:
我有几个模板类型,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.
};
我想在QuietNest<Eggs...> 上有一个hatch 方法来产生一个LoudNest。对于 QuietNest 中的每个 Egg<T>,LoudNest 应该有一个 Chick<T>。我有一个函数QuietNest<Eggs...>::hatch_impl,它可以创建一个std::tuple<Chicks...>,其中Chicks 都有正确的类型参数。也就是说,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