[英]Check if class is of template specialization (with template arguments like bool or int)
基於如何判斷模板類型是否是模板類的實例? 並檢查類是否是模板專業化? 我創建了以下變體來檢查MyClass1
、 MyClass2
或MyClass3
特定實例:
template <class T, template <class...> class Template>
constexpr bool is_instance_of_v = false;
template <template <class...> class Template, class... Args>
constexpr bool is_instance_of_v<Template<Args...>, Template> = true;
template<class T> struct MyClass1 { };
template<class T, class B> struct MyClass2 { };
template<class T, bool B> struct MyClass3 { };
int main(int argc, char* argv[])
{
constexpr bool b1 = is_instance_of_v<MyClass1<float>, MyClass1>;
constexpr bool b2 = is_instance_of_v<MyClass1<float>, MyClass2>;
// constexpr bool b3 = is_instance_of_v<MyClass1<float>, MyClass3>; // <-- does not compile
return 0;
}
但是, b3
的代碼無法編譯並出現以下錯誤:
error C3201: the template parameter list for class template 'MyClass3' does not match the
template parameter list for template parameter 'Template'
error C2062: type 'unknown-type' unexpected
這似乎是因為來自MyClass3
的bool
參數不是class
,因此無法通過template <class...> class Template
捕獲。
有沒有辦法解決這個問題,以便它適用於任何模板參數列表(不僅是class
,還有bool
、 int
等)?
沒有通用模板來處理類型參數和非類型參數(和模板模板參數)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.