部分定义/别名模板模板参数
Partially defining / aliasing a template template parameter
我正在尝试将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>
{};
演示
相关文章:
- 部分定义/别名模板模板参数
- 告诉c++编译器该参数没有别名
- 为什么我们不能重复使用具有不同模板参数的别名模板标识符?
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- 模板模板参数和模板别名:编译器错误?
- 为模板参数包添加别名
- C++使用默认模板参数键入别名和转发声明
- 缺少别名模板C++参数列表
- 使用外部定义的模板类型作为模板参数的更通用模板的模板别名
- 必须非常量别名参数及其默认参数常量
- 作为模板参数 c++ 给出的类的别名模板
- 使用其他模板类型参数作为要在函数签名中使用的类型别名声明
- 参数化类的别名(或类型定义)内部类
- C++别名的模板参数包扩展
- 如何简化模板模板参数中的enable_if别名
- 包含可变参数包的第一个可转换类型的别名的结构
- C++ 11 个模板,参数包的别名
- 为什么模板引用类型不能用作模板类型别名参数?
- 具有模板类默认参数的 C++17 别名模板
- 如何用可变数量的参数别名一个函数