C++与任何类型的参数匹配的可变参数模板模板参数

C++ variadic template template argument that matches any kind of parameters

本文关键字:参数 变参 任何 类型 C++      更新时间:2023-10-16

我想知道是否可以编写一个模板函数,该函数可以将任何其他任意模板作为参数并正确匹配模板名称(即不仅仅是结果类(。我所知道的工作是这样的:

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_ttypenames,这种方法是行不通的。因此,匹配任何内容所需的内容将是这样的,其中对省略号中允许的内容完全没有限制:

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);

那么,有什么方法可以使用模式匹配以完全通用的方式使其工作吗?

这不仅仅是一个思想实验,因为我被困的用例是创建在容器上运行的纯函数原语,并将隐式构造不可变的结果容器。如果结果容器具有不同的数据类型,我们需要知道容器操作的类型,因此对任何容器的唯一要求是模板的第一个参数必须是输入类型,以便可以在结果中将其替换为不同的输出类型,但代码应该忽略之后的任何模板参数,并且不应该关心它是否是一种类型或值。

您感兴趣的构造有两个级别,其中包含可变参数模板。

  • 函数模板的外部可变参数模板参数列表TemplatePSizes
  • 内部参数包作为模板模板参数
  • 的模板参数 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特征。
在那之前,我们一直专注于。