使用来自另一个模板的参数对模板进行柯里化
Currying template with parameter from another template
我有 class Foo
,它有两个模板参数,A
和 B
:
template<typename A, typename B>
struct Foo {};
我还有classBase
,它有一个模板模板参数:
template<template<typename B> typename Foo>
struct Base {};
我想写 class Derived
假设如下:
Derived
有一个模板参数 (A
)
Derived
扩展 class Base
Derived
作为模板参数传递给 class Base
class Foo
,但带有一个参数 "currying" (A
)
我该怎么做?
这是我的(not working)解决方案:
template<template<typename B> typename Foo>
struct Base {};
template<typename A, typename B>
struct Foo {};
template<template<typename A, typename B> typename Foo, typename A>
struct BindFirst {
template<typename B>
using Result = Foo<A, B>;
};
template<typename A>
struct Derived : Base<
// error is here
typename BindFirst<Foo, A>::Result
> {};
这给我错误:
template argument for template template parameter must be a class template or type alias template
模板 Base
需要一个模板作为第一个参数,但您试图传递依赖类型(由 typename
表示),因此出现错误消息。此外,BindFirst
中的嵌套别名 Result
是一个模板,因此需要一个模板参数才能与 typename
一起使用。所以而不是
typename BindFirst<Foo, A>::Result
你必须告诉编译器 Result
实际上是一个模板,使用
BindFirst<Foo, A>::template Result
我有 class Foo
,它有两个模板参数,A
和 B
:
template<typename A, typename B>
struct Foo {};
我还有classBase
,它有一个模板模板参数:
template<template<typename B> typename Foo>
struct Base {};
我想写 class Derived
假设如下:
Derived
有一个模板参数 (A
)Derived
扩展 classBase
Derived
作为模板参数传递给 classBase
classFoo
,但带有一个参数 "currying" (A
)
我该怎么做?
这是我的(not working)解决方案:
template<template<typename B> typename Foo>
struct Base {};
template<typename A, typename B>
struct Foo {};
template<template<typename A, typename B> typename Foo, typename A>
struct BindFirst {
template<typename B>
using Result = Foo<A, B>;
};
template<typename A>
struct Derived : Base<
// error is here
typename BindFirst<Foo, A>::Result
> {};
这给我错误:
template argument for template template parameter must be a class template or type alias template
模板 Base
需要一个模板作为第一个参数,但您试图传递依赖类型(由 typename
表示),因此出现错误消息。此外,BindFirst
中的嵌套别名 Result
是一个模板,因此需要一个模板参数才能与 typename
一起使用。所以而不是
typename BindFirst<Foo, A>::Result
你必须告诉编译器 Result
实际上是一个模板,使用
BindFirst<Foo, A>::template Result