2017-01-22 31 views
0

基於type_traits信息是否有可能產生函數/類模板過載?C + +有基於type_traits有模板重載的方法嗎?

例子:

#include <type_traits> 

template<typename Object> 
class object_worker 
{ 
public: 
    object_worker(Object&& o) // o - is not POD 
    { 
     // do something 
    } 
}; 

template<typename Object> 
class object_worker<std::is_pod<Object>::value == true> // how to make this thing work? 
{ 
public: 
    object_worker(Object &&o) // o - is POD 
    { 
     // do something different 
    } 
}; 
  • 是否有做一些有一定技術?像部分模板專業化
  • 如果可以實現這一點,它的名字是什麼? (例如模板偏特概念

回答

1

是的,你可以做這樣的事情。它使用非常廣泛。

template<typename T, bool = is_pod<T>::value>> 
class foo 
{ 
}; 

// This is a partial template specialization. 
// Triggered only when is_pod<T>::value is true 
template<typename T> 
class foo<T, true> // T can be only a POD type 
{ 
}; 
+0

保存我的一天!它不應該被稱爲_full specialization_ for第二個模板參數嗎?我想,_partial_就是這樣的:'template class cls {};' – andrgolubev

+0

只希望沒有人實例化'foo x;':-) –

+0

@KerrekSB Aye,通常最好是圍繞這個也許將它作爲* detail *基類。 – DeiDei