类的可变参数模板部分专用化,用于限制模板参数的类型
Variadic template partial specialization of a class to restrict type of template arguments
我有一个类Foo
需要具有可变数量的模板参数,但这些参数必须是某种泛型类型,而不是完全任意的。例如
template < int I, typename T> struct Arg;
using type1 = Foo<Arg<3, double>>;
using type2 = Foo<Arg<1, int>, Arg<7, float>, Arg<1, int>>;
我想知道实现这一目标的最佳方法是什么。我想我需要先从一个普通的可变参数模板开始
template < typename ...T >
class Foo;
从那里,我可以沿着递归之路
template < int I, typename T, typename ...Others>
template Foo<Arg<I, T>, Others...>
{
...
};
但是阅读另一个问题的答案让我想知道我对可变参数模板的了解以及如何有时避免递归。
我的问题是,模板参数应该采用相对严格的格式这一事实是否能够实现Foo
的部分特化,这不会是递归的,并且可以有效地处理形式Foo<Arg<...>,Arg<...>,...>
的所有Foo
?
这有效:
#include <iostream>
template <int i, typename T> struct Arg;
template <typename ...T>
class Foo;
template <int ...Is, typename ...Ts>
class Foo<Arg<Is, Ts>...>
{
public:
static constexpr unsigned int N = sizeof...(Is);
};
int main()
{
using type2 = Foo<Arg<1, int>, Arg<7, float>, Arg<1, int>>;
std::cout << type2::N << "n";
}
尽管以这种形式使用模板参数可能容易或不方便,具体取决于您要对它们执行的操作。
你可以用SFINAE做到这一点。 这是草图:
template<class...Bs>
constexpr bool is_all_true(Bs...); // write this
template<class T>
constexpr bool is_valid_arg(); // write this
template < class=void, class...Ts >
class FooImpl;
template < class...Ts >
class FooImpl<std::enable_if_t<is_all_true( is_valid_arg<Ts>()...) >, Ts...> {
// code
};
template<class...Ts>
class Foo:FooImpl<void, Ts...> {};
现在Foo
是一个FooImpl
,它测试你的前提条件是否都满足。
你必须编写is_all_true
和is_valid_arg
,其中is_valid_arg
测试T
是否是Arg<int, Type>
的形式。
举个例子,在 C++17 中,is_all_true
只是return (true && ... && bs);
(如果我没记错的话,true
是多余的,但为了清楚起见,我喜欢它(。 在 C++11/14 中,这将更加困难。
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 使用在用于SFINAE的void_t中具有参数的方法
- 用于检查值是否为其任何参数的帮助程序函数
- 为什么可变参数函数不适用于模板
- 为什么数组大小信息可用于"sizeof"运算符和 delete[] 运算符,但在将数组作为参数传递到
- 一个模板方法,用于同时接受常量和非常量参数
- Constexpr 可变参数模板,用于对无符号整数进行重新排序
- 使用一个参数的模板函数时出错(适用于 2)
- 为什么模板参数推导不适用于仅指定前两个参数的可变参数模板类?
- 隐式生成的函数重载用于右值参数?
- 为什么带有类型参数的运算符 () 可以应用于 result_of 上下文中的类型?
- 将非 const 对象用于 const 参数
- 我可以将模板中的类型参数用于非类型参数吗?
- opencl openGL互操作内核参数用于读取 写入
- 分配参数用于测试目的
- 将命令行参数用于输入参数
- 如何使用类的属性作为模板参数/用于专用化
- 为什么我不能将此参数用于 std::sort() 的比较?
- 模板类参数用于模板类函数语法
- 为什么 GCC 不允许我将模板参数用于另一个模板的参数?