Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Currying template with parameter from another template

I have class Foo, which has two template parameters, A and B:

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

Also I have class Base, which has one template template parameter:

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

I want to write class Derived assuming the following:

  • Derived has one template parameter (A)
  • Derived extends class Base
  • Derived passes as template parameter to class Base class Foo, but with one parameter "currying" (A)

How can I do this?


Here is my (not working) solution:

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

> {};

Which gives me error:

template argument for template template parameter must be a class template or type alias template

like image 444
diralik Avatar asked Nov 12 '18 22:11

diralik


Video Answer


1 Answers

The template Base expects a template as the first parameter but you attempt to pass a dependent type (indicated by typename), hence the error message. Moreover, the nested alias Result inside BindFirst is a template and therefore would require a template parameter for use with typename. So instead of

typename BindFirst<Foo, A>::Result

you have to tell the compiler that Result is in fact a template, using

BindFirst<Foo, A>::template Result

Live example

like image 56
Henri Menke Avatar answered Oct 26 '22 12:10

Henri Menke