[英]how to force instantiation of a template class used with a type alias
我有一個帶有大量模板參數的模板 class。
這是一個 helper class,僅用於預定義的類型列表。 我使用類型別名( using
或typedef
),這樣用戶就不會被模板后綴污染。
由於類型列表是硬編碼的,我想強制實例化 class T,但由於列表很長,我想強制實例化而不重復所有參數。
我怎么能那樣做?
template <class... X>
class T {
};
// alias the type so that the user don't get mad with type list
using Z = T<int, bool, char, long
/* possibly a long list of parameters*/ >;
// this works but needs to repeat all template arguments
template class T<int, bool, char, long>;
// how to force instantiation of T using Z?
您可以使用允許將所有模板參數指定為單個std::tuple
的特化:
#include <tuple>
template <class... X>
class T {};
template <class ...X>
class T<std::tuple<X...>> : public T<X...> {};
using types = std::tuple<int,bool,char,long>;
using Z = T<types>;
template class T<types>;
作為一種解決方法,我使用了宏...
#define MY_TYPE T<int,bool,char,long>
using Z = MY_TYPE;
template class MY_TYPE;
我還在尋找更好的解決方案...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.