代码之家  ›  专栏  ›  技术社区  ›  diralik

使用另一个模板中的参数转换模板

  •  6
  • diralik  · 技术社区  · 6 年前

    Foo ,它有两个模板参数, A B :

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

    我还有课 Base ,其中有一个模板参数:

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

    我想写一节课 Derived 假设如下:

    • 派生 有一个模板参数( A. )
    • 派生 扩展类
    • 派生 作为模板参数传递给类 基础 ,但只有一个参数“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
    
    > {};
    

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

    1 回复  |  直到 6 年前
        1
  •  2
  •   Henri Menke    6 年前

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

    typename BindFirst<Foo, A>::Result
    

    你必须告诉编译器 结果

    BindFirst<Foo, A>::template Result
    

    Live example