2012-12-21 46 views
1

如果我做的:如何斷點多個C++模板方法,就像非模板一樣?

(gdb) b nmspace::TestClass::compareFOO 

然後下面的方法簽名將作爲斷點簽名:

nmspace::TestClass::compareFOO(blah::Foo const&, blah::Foo const&, unsigned int) 
nmspace::TestClass::compareFOO(blah::Foo const&, blah::FooField const&, unsigned int) 
nmspace::TestClass::compareFOO(blah::FooField const&, blah::Foo const&, unsigned int) 
nmspace::TestClass::compareFOO(blah::FooField const&, blah::FooField const&, unsigned int) 

有類似下面的內容,否則我們必須寫四個每次模板方法?多態還不適用於C++模板嗎?

nmspace::TestClass::compareFOOES<blah::Foo, blah::Foo> 
nmspace::TestClass::compareFOOES<blah::Foo, blah::FooField> 
nmspace::TestClass::compareFOOES<blah::FooField, blah::Foo> 
nmspace::TestClass::compareFOOES<blah::FooField, blah::FooField> 

我試過nmspace::TestClass::compareFOOESnmspace::TestClass::compareFOOES*nmspace::TestClass::compareFOOES<>()

回答

1

如果所有的函數體是相同的你可能寫一個全局函數可以接受多種類型的使用std::enable_ifboost::enable_if參數:

template< class T > 
struct is_valid_field 
    : boost::or_<boost::is_same<T, blah::Foo>, boost::is_same<T, blah::FooField>> 
{ 
}; 

template< class T, class Q > 
nmspace::TestClass::compareFOO(T const&, Q const&, unsigned int, 
    typename boost::enable_if< 
     boost::and_<is_valid_field<T>, is_valid_field<Q> 
    >::type* = 0) 
{ 
    // implementation 
} 
+0

非常有用,謝謝。 –