[英]Daisy chain variadic templated classes
I have a class template Pipeline: 我有一个类模板管道:
template <typename A, typename B> class Pipeline;
I'd like to create a variadic function template accepting an arbitrary number of arbitrary Pipeline
s, and I'd like to constrain those in a tricky way. 我想创建一个可变参数函数模板,以接受任意数量的任意Pipeline
,并且希望以一种棘手的方式来约束那些模板。 Non variadic code would look like: 非可变参数代码如下所示:
Pipeline<A, C> compose(Pipeline<A, B> p1, Pipeline<B, C> p2);
Pipeline<A, D> compose(Pipeline<A, B> p1, Pipeline<B, C> p2, Pipeline<C, D> p3);
// ...and so on
Now is it possible at all to constrain them end-to-end in a variadic way? 现在是否有可能以可变的方式端到端地约束它们?
// I would like to write something like:
Pipeline<Args[0], Args[len(Args)-1]> compose(Pipeline<Args[i], Args[i+1]> ps...);
I'll assume that your Pipeline
looks something like: 我假设您的Pipeline
看起来像这样:
template <typename A, typename B>
struct Pipeline {
using first = A;
using second = B;
};
First, let's make a type trait for what's a valid chain link: 首先,让我们为有效的链链接建立类型特征:
template <typename P1, typename P2>
struct is_valid_link : std::false_type { };
template <typename A, typename B, typename C>
struct is_valid_link<Pipeline<A,B>, Pipeline<B,C>> : std::true_type { };
Next, let's borrow @Columbo's bool_pack
trick to verify that bunch of bool
s are all true
: 接下来,让我们借用bool_pack
的bool_pack
技巧,以验证一堆bool
都是true
:
template <bool...> struct bool_pack;
template <bool... v>
using all_true = std::is_same<bool_pack<true, v...>, bool_pack<v..., true>>;
And of course we need the index sequence trick: 当然,我们需要索引序列的技巧:
template <typename... Pipelines,
typename R = decltype(detail::daisy_chain(
std::make_index_sequence<sizeof...(Pipelines)-1>(),
std::declval<Pipelines>()...))
>
R compose(Pipelines... pipelines)
{
return {};
}
Where the bulk of the work is doing the checking here: 大部分工作在这里进行检查:
namespace detail {
template <size_t... Is,
typename... Pipelines,
typename T = std::tuple<Pipelines...>,
typename R = std::enable_if_t<
// ensure that all our pairwise pipelines are valid links
all_true<
is_valid_link<std::tuple_element_t<Is,T>,
std::tuple_element_t<Is+1,T>>::value...
>::value,
// pick out the first and last types
Pipeline<typename std::tuple_element_t<0, T>::first,
typename std::tuple_element_t<sizeof...(Pipelines)-1, T>::second>
>>
R daisy_chain(std::index_sequence<Is...>, Pipelines... pipelines);
}
That lets us do: 这让我们可以:
int main() {
Pipeline<int, double> p = compose(Pipeline<int, char>{}, Pipeline<char, double>{});
}
The advantage of writing it this way is that you still have SFINAE - if that's something you want. 用这种方式编写的好处是您仍然拥有SFINAE-如果您想要这样的话。 Such that: 这样:
auto invalid = compose(Pipeline<int, char>{}, Pipeline<float, double>{});
will trigger an overload resolution failure: 将触发重载解析失败:
main.cpp:46:31: error: no matching function for call to 'compose'
Pipeline<int, double> p = compose(Pipeline<int, char>{}, Pipeline<float, double>{});
^~~~~~~
main.cpp:40:3: note: candidate template ignored: substitution failure [with Pipelines = <Pipeline<int, char>, Pipeline<float, double>>]: no matching function for call to 'daisy_chain'
R compose(Pipelines... pipelines)
^
1 error generated.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.