[英]specialization of a template who has a parameter
我得到了以下模板
template<class T, class U, bool(*function)(T)>
struct foo {
/.../
}
我如何為它創建一個專業化? 專業化應該如下
template<>
struct foo<my_type_1, my_type_2,/*how do I handle this parameter?*/>
我的主要問題是如何處理函數的參數?
提前致謝
您可以像處理模板參數列表中的任何其他參數一樣處理它。 T
是一個 class (或其他類型),所以你提供給它這樣的( my_type_1
)。 U
也是如此。 最后一個參數是 function ,它接受T
類型的 object (在您的專業中是my_type_1
)並返回一個bool
。 所以給它提供這樣的:
template<class T, class U, bool(*function)(T)>
struct foo {};
struct my_type_1 {};
struct my_type_2 {};
bool my_func(my_type_1 m) {
return true;
}
template<>
struct foo<my_type_1, my_type_2, my_func> {};
只需添加您的 function 和 my_type_1 作為參數和 bool 作為返回:
bool poo(my_type_1 X){
return 0;
}
所以你的代碼必須編譯:
struct foo<my_type_1, my_type_2, poo>
假設:
// Your primary template
template <class T, class U, bool(*function)(T)>
struct foo;
// Some classes
struct my_type_1;
struct my_type_2;
// A function
bool my_func(my_type_1);
對於部分專業化,它將是:
template <bool (*func)(my_type1)>
struct foo<my_type_1, my_type_2, func> { /*..*/};
對於完全專業化:
template <>
struct foo<my_type_1, my_type_2, &my_func> { /*..*/};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.