简体   繁体   English

从默认参数推导出参数包

[英]Deduce parameter pack from default argument

Is it possible for the compiler to deduce a parameter pack from a function's default aargument?编译器是否可以从函数的默认参数中推断出参数包? Particularly, I have the following code:特别是,我有以下代码:


template <int ... Is> struct seq {};
template <int ... Is> struct make_seq;
template <int head, int ... tail>
struct make_seq<head, tail...>
{
    using type = typename make_seq<head - 1, head - 1, tail...>::type;
};
template <int ... Is>
struct make_seq<0, Is...>
{
    using type = seq<Is...>;
};
template <int N>
using make_seq_t = typename make_seq<N>::type;

template<int N, int ...Is>
int deduceParamPack(seq<Is...> s = make_seq_t<N>{})
{
    return sizeof...(Is);
}

int main()
{
    return deduceParamPack<5>();
}

And the compiler deduces the parameter pack as empty, and tries to cast the default argument to it.并且编译器将参数包推断为空,并尝试将默认参数转换为它。 Instead, I would like to achieve a similar behaviour to:相反,我想实现类似的行为:

int main()
{
    return deduceParamPack<5>(make_seq_t<5>{});
}

where the deduced parameter pack is 0,1,2,3,4 , without explicitly passing in this argument.其中推导的参数包是0,1,2,3,4 ,没有显式传入这个参数。

Is it possible for the compiler to deduce a parameter pack from a function's default aargument?编译器是否可以从函数的默认参数中推断出参数包?

No, as far I know.不,据我所知。

But... not exactly what you asked... but maybe you can find useful the following solution based in partial specialization of structs但是......不完全是你问的......但也许你可以找到有用的以下基于结构部分专业化的解决方案

template <std::size_t N, typename = std::make_index_sequence<N*N>>
struct deduceParamPackStruct;

template <std::size_t N, std::size_t ... Is>
struct deduceParamPackStruct<N, std::index_sequence<Is...>>
 {
   static constexpr std::size_t func ()
    { return sizeof...(Is); }
 };

You can use it as follows您可以按如下方式使用它

static_assert( 25 == deduceParamPackStruct<5>::func() );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM