【问题标题】:Currying template with parameter from another template使用来自另一个模板的参数的 Currying 模板
【发布时间】:2018-11-12 22:09:19
【问题描述】:

我有类Foo,它有两个模板参数AB

template<typename A, typename B>
struct Foo {};

我还有类Base,它有一个模板模板参数:

template<template<typename B> typename Foo>
struct Base {};

我想写 Derived 类,假设如下:

  • Derived 有一个模板参数 (A)
  • Derived 扩展类 Base
  • Derived 作为模板参数传递给BaseFoo,但有一个参数“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

> {};

这给了我错误:

模板参数的模板参数必须是类模板或类型别名模板

【问题讨论】:

  • BindFirst&lt;Foo, A&gt;::template Result(不是typename,它是一个模板)。
  • 现场演示:gcc.godbolt.org/z/RpsRbT

标签: c++ templates metaprogramming currying


【解决方案1】:

模板Base 需要一个模板作为第一个参数,但您尝试传递一个依赖类型(由typename 指示),因此出现错误消息。此外,BindFirst 内的嵌套别名Result 是一个模板,因此需要一个模板参数才能与typename 一起使用。所以不是

typename BindFirst<Foo, A>::Result

你必须告诉编译器Result实际上是一个模板,使用

BindFirst<Foo, A>::template Result

Live example

【讨论】:

    猜你喜欢
    • 2014-04-16
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 2011-08-28
    • 2021-12-21
    相关资源
    最近更新 更多