部分定义/别名模板模板参数

Partially defining / aliasing a template template parameter

本文关键字:参数 别名 定义      更新时间:2023-10-16

我正在尝试将CRTP和模板模板参数与模板化派生类一起使用,并在传递给基类进行完全定义之前指定它的一些但不是全部参数。我能将其与之进行比较的最接近的概念是模板化别名,但由于它必须都在类定义的顶部一行,我不确定如何实现这一点。希望一个例子能让它更清楚一点。。。

这是我到目前为止的代码:

template<template<typename> class Template1, typename Param1>
class Base
{
public:
using type = Param1;
};
template<template<typename, typename> class Template1, typename Param1, typename Param2>
class Derived : public Base<template<typename P1> class Template1<P1, Param2>, Param1>
{};
template<typename Param1, typename Param2>
class Template1
{};
int main()
{
Derived<Template1, int, double>::type d = 0;
}

此操作当前失败,原因如下:

9:89:错误:模板参数的数目错误(1,应为2(2:7:错误:为'template<模板类Template1,类Param1>类Base'在函数"int main(("中:18:3:错误:"type"不是"Derived<Template1,int,双精度>'

这个错误消息真正让我困惑的是,我看不到任何地方只指定了一个模板参数。我还发现,如果我定义Derived如下,那么它编译得很好:

template<typename> class Test {};
template<template<typename, typename> class Template1, typename Param1, typename Param2>
class Derived : public Base<Test, Param1>
{};

我认为这表明问题肯定在这条线上(毫不奇怪,这是我不清楚如何实现的一点(:

class Derived : public Base<template<typename P1> typename Template1<P1, Param2>, Param1>

基本上,在这里,我试图用一个自变量定义一个新模板,这是对第一个有两个自变量的模板的部分专业化。我想我做得不对。但是我怎么能在一条线上呢?

提前感谢您的帮助。如果这在任何方面都不清楚,请道歉!

可能是这样的:

template<template<typename, typename> class TwoParamTemplate, typename Param2>
struct BindSecond {
template <typename Param1>
using type = TwoParamTemplate<Param1, Param2>;
};
template<template<typename, typename> class Template1,
typename Param1, typename Param2>
class Derived : public Base<BindSecond<Template1, Param2>::template type, Param1>
{};

演示