[英]Partial template specialization, using a templatized type (nested template classes)
給定通用函數foo:
template<typename T> void foo(T a) { a(); }
我想專門針對Bar類型使用此函數,但是Bar本身具有多個模板參數。 我試圖按如下方式專門化foo():
template<typename... Args> void foo<Bar<Args...> >(Bar<Args...> a) { a(42); }
但是,這不太起作用。 有人可以幫我嗎? 謝謝
沒有功能模板的部分專業化之類的東西。 一種解決方法是委托一個類模板,該模板實際上可以是部分專用的。 遵循以下原則:
template <typename T>
struct FooImpl {
static void foo(T a) { /* general implementation */ }
};
template<typename... Args>
struct FooImpl<Bar<Args...>> {
static void foo(Bar<Args...> a) { /* special implementation */ }
};
template<typename T>
void foo(T a) { FooImpl<T>::foo(a); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.