从默认参数推导参数包
Deduce parameter pack from default argument
编译器是否可以从函数的默认参数中推断出参数包?特别是,我有以下代码:
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>();
}
编译器将参数包推断为空,并尝试向其强制转换默认参数。相反,我想实现与以下类似的行为:
int main()
{
return deduceParamPack<5>(make_seq_t<5>{});
}
其中推导的参数包是0,1,2,3,4
,而不显式传入此参数。
编译器是否可以从函数的默认参数中推断出参数包?
不,据我所知。
但。。。不完全是你问的...但也许您可以找到以下基于结构部分专业化的解决方案
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); }
};
您可以按如下方式使用它
static_assert( 25 == deduceParamPackStruct<5>::func() );
相关文章:
- 使函数参数默认为周围范围
- 如何将值传递给其参数(默认参数)
- 当“std::make_index_sequence”和“std::index_sequence”用于模板参数默认类型时
- C ,构造器中允许的参考参数默认值
- 哪些规则控制参数默认赋值?
- 错误:上一个规范后给出的参数默认参数
- 是否可以设计一个包含模板参数默认值的类
- 构造函数中参数C++默认值
- 如何设置依赖于其他参数的参数默认值
- C++模板参数默认函数实现
- QInput对话框参数默认值
- g++4.8.2上列表方法参数默认初始化时出错
- 在函数定义中指定参数默认值会导致错误 C2143:语法错误:'='之前缺少')'
- 函数模板:将第一个模板参数默认为第二个
- 模板模板参数默认可以引用其他模板类型的参数
- 参数默认为先例参数的值
- 如何修改一个类,使它只有一个成员函数,所有参数默认
- 内置类型的模板参数默认值
- 是否可以使用构造函数或对象作为其他类方法的参数默认值?
- 模板参数默认为更高版本