[英]Adapting a template with a non-type parameter as a template template parameter
我有一個模板,可以簡化為以下內容
template <typename T, template <typename U> class Base>
class Derived: Base<T> {
};
作為Base
我希望能夠使用可能具有非模板參數的模板。 例如
template <unsigned N, typename T>
struct NBase {
};
這顯然不能直接工作,因為它的參數與Base
的單個參數不匹配,所以我想我會做類似的事情
template <unsigned S>
struct NAdapter {
template <typename T>
using B = NBase<S, T>;
};
這種作品,例如這個編譯:
void f() {
Derived<int, NAdapter<100>::B> a;
}
然而,這不能編譯
template <unsigned M>
void eval() {
Derived<int, NAdapter<M>::B> b;
}
由此產生的錯誤是note: expected a class template, got 'NAdapter<M>::B'
。
有沒有辦法將NBase
調整為Base
以便在兩種情況下都可以使用?
解決方案
這有效,謝謝@mutableVoid 的建議!
template <unsigned M>
void eval() {
Derived<int, NAdapter<M>::template B> b;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.