简体   繁体   中英

Adapting a template with a non-type parameter as a template template parameter

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM