[英]Pass inner template class as template argument in C++
我試圖將內部模板類(常規類)傳遞給另一個接受模板作為參數的類。
接受模板的類是:
template <typename... T> struct TypeList {
template <template <typename...> typename P> using Apply = P<T...>;
};
所以當我using List = TypeList<int, float, double>;
我可以
static_assert(std::is_same<List::template Apply<std::tuple>,
std::tuple<int, float, double>>::value);
但是,如果我將std::tuple
更改為內部模板類,它將停止工作。 IE,
struct Outer {
template <typename... P> struct Inner {};
};
static_assert(std::is_same<List::template Apply<typename Outer::Inner>,
Outer::Inner<int, float, double>>::value);
不起作用。
我的編譯器抱怨
錯誤:在沒有參數列表的情況下無效使用模板名稱“Outer::Inner”
如果我template <typename... P> using Flat = Outer::Inner<P...>;
用template <typename... P> using Flat = Outer::Inner<P...>;
“展平”內部模板類,它會起作用template <typename... P> using Flat = Outer::Inner<P...>;
.
我的問題是,有沒有一種方法可以使內部模板類工作,而無需對其進行別名和展平? 我是否在某處缺少typename
或template
關鍵字?
完整的例子是:
#include <tuple>
#include <type_traits>
template <typename... T> struct TypeList {
template <template <typename...> typename P> using Apply = P<T...>;
};
struct Outer {
template <typename... P> struct Inner {};
};
template <typename... P> using Flat = Outer::Inner<P...>;
int main() {
using List = TypeList<int, float, double>;
static_assert(std::is_same<List::template Apply<std::tuple>,
std::tuple<int, float, double>>::value);
static_assert(std::is_same<List::template Apply<Flat>,
Outer::Inner<int, float, double>>::value);
static_assert(std::is_same<List::template Apply<typename Outer::Inner>,
Outer::Inner<int, float, double>>::value);
}
typename Outer::Inner
是錯誤的,因為Inner
不是類型而是模板。
您甚至可以在此處刪除所有類型名/模板,因為沒有相關類型問題。
static_assert(std::is_same<List::Apply<Outer::Inner>,
Outer::Inner<int, float, double>>::value);
在依賴上下文中,它將是
// template <typename OuterT> /*..*/
static_assert(std::is_same<List::Apply<OuterT::template Inner>,
typename OuterT::template Inner<int, float, double>>::value);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.