Reputation: 2127
I have a couple of templated types, Egg<T>
and Chick<T>
.
template<typename T>
struct Egg{};
template<typename T>
struct Chick{};
The chicks are contained in a class LoudNest<Chicks...>
and the eggs in QuietNest<Eggs...>
:
template <typename... Chicks>
struct LoudNest {
std::tuple<Chicks...> chicks;
};
template <typename... Eggs>
struct QuietNest {
std::tuple<Eggs...> eggs;
// more here.
};
I want to have a hatch
method on QuietNest<Eggs...>
that produces a LoudNest. The LoudNest should have a Chick<T>
for each Egg<T>
in the QuietNest. I have a function QuietNest<Eggs...>::hatch_impl
that can create a std::tuple<Chicks...>
where the Chick
s all have the correct type parameters. That is, QuietNest<Egg<double>, Egg<string>, Egg<char>>::hatch_impl
will return std::tuple<Chick<double>, Chick<string>, Chick<char>>
. I'm getting stuck trying to wrap that in a LoudNest
constructor:
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>{}; }
};
I'm thinking I need to make a "converter" that accepts a parameter pack of eggs and produces a LoudNest with chicks of the corresponding types. Starting with converting a single Egg<T>
to a Chick<T>
, I have:
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>;
};
Where I'm getting stuck is when I try to do the same for a parameter pack:
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");
And try number two:
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?
My actual problem has to do with implementing an interpreter, and the classes are FormalParameter<T>
(Egg<T>
), ActualParameter<T>
(Chick<T>
), etc. However, I wanted to avoid using the word "parameter" for in the example code, as we're already talking about Parameter Packs in a different sense.
code from this post: https://godbolt.org/z/XBIEhm
Upvotes: 1
Views: 80
Reputation: 118691
I was able to fix your example with a few changes: https://godbolt.org/z/3VW68f
Add a deduction guide to LoudNest
to alleviate the problem deducing the Chicks...
types in LoudNest{hatchlings}
(this probably isn't the only solution, but seemed clean in that it didn't require complicating the hatch() implementation):
template<typename... Chicks>
LoudNest(const std::tuple<Chicks...>& chicks) -> LoudNest<Chicks...>;
(Add hatchOne
which was present in your question but not the godbolt link you shared)
Get rid of hatch_impl
in favor of just calling hatchOne
during pack expansion:
auto hatchlings = std::apply(
[&](auto... args) { return std::make_tuple(hatchOne(args)...); },
eggs);
Use a specialization to deduce the inner T
types of the Egg parameters to AsLoudNest1
:
template<typename... Eggs>
struct AsLoudNest1 {};
template<typename... Ts>
struct AsLoudNest1<Egg<Ts>...> {
using type = LoudNest<Chick<Ts>...>;
};
Upvotes: 2