2

I have a template which can be reduced to the following

template <typename T, template <typename U> class Base>
class Derived: Base<T> {
};

As Base I would like to be able to use a template which may have a non-template parameter. For example

template <unsigned N, typename T>
struct NBase {
};

This obviously won't work directly as its parameters do not match the single parameter of Base, so I thought I would do something like

template <unsigned S>
struct NAdapter {

   template <typename T>
   using B = NBase<S, T>;
};

This kind of works, for instance this compiles:

void f() {
   Derived<int, NAdapter<100>::B> a;
}

This however doesn't compile

template <unsigned M>
void eval() {
  Derived<int, NAdapter<M>::B> b;
}

The resulting error is note: expected a class template, got ‘NAdapter<M>::B’.

Is there a way to adapt NBase as Base so it would work in both cases?

Solution

This works, thank you @mutableVoid for your suggestion!

template <unsigned M>
void eval() {
  Derived<int, NAdapter<M>::template B> b;
}
kesha_r
  • 31
  • 3
  • check out [this post](https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords): from looking at it, it seems to me that you have to add 'template' in front of NAdapter::B – mutableVoid Jan 27 '20 at 22:43
  • Unfortunately this doesn't work, now the error is "expected a class template, got ‘typename NAdapter::B’" – kesha_r Jan 27 '20 at 22:53
  • 1
    [Works for me](https://wandbox.org/permlink/yZQSh8hmFbIHJAGA) Edit: I miswrote in my original comment, template has to be placed in front of B – mutableVoid Jan 27 '20 at 23:00
  • 1
    It does indeed work, thank you very much! – kesha_r Jan 28 '20 at 00:53
  • Don’t edit answers into your question. – Davis Herring Jan 28 '20 at 03:53

1 Answers1

0

This is core issue 1478; some implementations require ::template here, as pointed out in the comments. The most current plan (mine) is to require this case to work without template and to deprecate its unnecessary use (at variance with its parsing purpose) there. It may be some time before implementations follow that rule, since it hasn’t even been reviewed yet.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76