C++与任何类型的参数匹配的可变参数模板模板参数
C++ variadic template template argument that matches any kind of parameters
我想知道是否可以编写一个模板函数,该函数可以将任何其他任意模板作为参数并正确匹配模板名称(即不仅仅是结果类(。我所知道的工作是这样的:
template<template<typename ...> class TemplateT, typename... TemplateP>
void f(const TemplateT<TemplateP...>& param);
例如,这将匹配f(std::vector<int>())
或f(std::list<int>())
但不适用于f(std::array<int, 3>())
,因为第二个参数是size_t
并且没有类型。
现在我想一个人可以做一些疯狂的事情,比如:
template<template<typename ...> class TemplateT, size... Sizes, typename... TemplateP>
void f(const TemplateT<Sizes..., TemplateP...>& param);
希望编译器能够正确地派生TemplateP
省略号或Sizes
省略号为空。但它不仅丑陋,而且还仅适用于采用类型或size_t
参数的模板。它仍然无法匹配任意模板,例如具有bool
参数。
重载方法也是如此:
template<template<typename ...> class TemplateT, typename... TemplateP>
void f(const TemplateT<TemplateP...>& param);
template<template<typename ...> class TemplateT, size... Sizes>
void f(const TemplateT<Sizes...>& param);
此外,如果我们想混合size_t
和typenames
,这种方法是行不通的。因此,匹配任何内容所需的内容将是这样的,其中对省略号中允许的内容完全没有限制:
template<template<...> class TemplateT, ... Anything>
void f(const TemplateT<Anything...>& param);
该语法不起作用,但也许还有其他语法可以定义这样的东西?
这主要是我想知道语言中有什么可能,认为它实际上可能有用途,如果您有不同的模板,其中第一个参数始终是固定的,并且您想根据返回类型更改它并保留其他所有内容。像这样:
template<
template<typename ValueT, ...> class TemplateT,
... Anything,
typename ValueT,
typename ResultT = decltype(some_operation_on_value_t(std::declval<ValueT>())>
TemplateT<ResultT, Anything...> f(const TemplateT<ValueT, Anything...>& in);
那么,有什么方法可以使用模式匹配以完全通用的方式使其工作吗?
这不仅仅是一个思想实验,因为我被困的用例是创建在容器上运行的纯函数原语,并将隐式构造不可变的结果容器。如果结果容器具有不同的数据类型,我们需要知道容器操作的类型,因此对任何容器的唯一要求是模板的第一个参数必须是输入类型,以便可以在结果中将其替换为不同的输出类型,但代码应该忽略之后的任何模板参数,并且不应该关心它是否是一种类型或值。
您感兴趣的构造有两个级别,其中包含可变参数模板。
- 函数模板的外部可变参数模板参数列表
TemplateP
和Sizes
内部参数包作为模板模板参数 - 的模板参数
TemplateT
,类模板
首先,让我们看一下内部TemplateT
类:为什么省略号运算符不能不匹配类似TemplateT< int, 2 >
?好吧,该标准在 §14.5.3 中将可变参数模板定义为
template<class ... Types> struct Tuple { };
template<T ...Values> struct Tuple2 { };
其中,第一种情况下的模板参数包只能匹配类型,而在第二种版本中只能匹配类型T
的值。特别
Tuple < 0 > error; // error, 0 is not a type!
Tuple < T, 0 > error2; // T is a type, but zero is not!
Tuple2< T > error3; // error, T is not a value
Tuple2< T, 0 > error4; // error, T is not a value
都是畸形的。此外,不可能回退到类似的东西
template<class ... Types, size_t ...Sizes> struct Tuple { };
因为标准在 §14.1.11 中规定:
如果主类模板或别名模板的模板参数是模板参数包,则 应为最后一个模板参数。不得遵循函数模板的模板参数包 通过另一个模板参数,除非可以从参数类型列表中推导出该模板参数 函数模板或具有默认参数 (14.8.2(。
换句话说,对于类模板,定义中只能出现一个可变参数包。因此,上述(双(可变参数类定义格式不正确。因为内部类总是需要这样的组合,所以不可能写出你想象的那么通用的东西。
什么可以拯救?对于外部函数模板,可以将一些分片放在一起,但您不会喜欢它。只要可以从第一个参数包推导出第二个参数包,就可能出现两个参数包(在函数模板中(。因此,诸如
template < typename... Args, size_t... N > void g(const std::array< Args, N > &...arr);
g(std::array< double, 3 >(), std::array< int, 5>());
是允许的,因为可以推导出整数值。当然,这必须专门针对每种容器类型,并且与您想象的相去甚远。
您必须有一个重新绑定容器类型的元函数。因为不能只替换第一个模板参数:
vector<int, allocator<int> > input;
vector<double, allocator<int> > just_replaced;
vector<double, allocator<double> > properly_rebound;
因此,只需为已知的容器集编写这样一个元函数即可。
template<class Container, class NewValue> class rebinder;
// example for vectors with standard allocator
template<class V, class T> class rebinder< std::vector<V>, T > {
public:
typedef std::vector<T> type;
};
// example for lists with arbitrary allocator
template<class V, class A, class T> class rebinder< std::list<V,A>, T > {
typedef typename A::template rebind<T>::other AT; // rebind the allocator
public:
typedef std::list<T,AT> type; // rebind the list
};
// example for arrays
template<class V, size_t N> class rebinder< std::array<V,N>, T > {
public:
typedef std::array<T,N> type;
};
不同容器的重新绑定规则可能有所不同。
此外,您可能需要一个从任意容器中提取值类型的元函数,而不仅仅是符合 std 的 ( typedef *unspecified* value_type
(
template<class Container> class get_value_type {
public:
typedef typename Container::value_type type; // common implementation
};
template<class X> class get_value_type< YourTrickyContainer<X> > {
......
public:
typedef YZ type;
};
如果我们有这样的东西,那就太棒了,因为它可以让我们在轻而易举地写出一个is_same_template
特征。
在那之前,我们一直专注于。
- 在不传递参数数量且只有3个点的情况下,如何使用变差函数
- 如何使用可变参数模板强制转换每个变体类型
- 关于如何在具有单个参数的变体构造中选择替代方案?
- 调用参数排列不变函数 f(i++, i++)
- 参数归纳与标准::变体
- 模板化回调参数的逆变,如 C# 中的逆变
- 如何在没有参数包的情况下编写变差函数
- 通过具有嵌套类的工厂类获取多个变异类模板参数包
- 获取模板参数的成员变量值列表
- 保留短 lambda 用作函数的中间参数,使用 clang 格式保持不变
- 如何定义变体<x,y,z>提取模板参数的子类型
- 正确对齐内存模板,参数顺序不变
- 递归中不同参数类型的变元模板函数
- 通过函数指针传递给变差函数的参数会更改其值
- 提升预定义为带有参数的全局 lambda 的变体访问者
- 使用可变参数模板参数提升变体访问者
- boost ::变体 - 为什么模板参数比const字符串参数具有更高的优先级
- 将变参数包中的值加载到临时数组中
- 使用额外参数提升变体访客
- 正在将动态数组元素解析为参数?(变音符)