下面的代碼,它試圖專門類模板「特殊」的基礎上,成員函數指針類型的返回類型,導致編譯錯誤與VC9:類模板部分特例參數化上的成員函數的返回類型
template<class F> struct special {};
template<class C> struct special<void(C::*)()> {};
template<class R, class C> struct special<R(C::*)()> {};
struct s {};
int main()
{
special<void(s::*)()> instance;
return 0;
}
錯誤C2752: '特殊':一個以上的部分特匹配
相同的代碼是由GCC-4.3.4接受,如圖中的模板的參數列表:http://ideone.com/ekWGg
這是VC9中的一個錯誤嗎?如果是這樣,這個錯誤會持續到VC10嗎?
我已經然而想出一個窘況侵入性的解決方法(對於這個特定的使用情況下,至少更普遍的解決方案表示歡迎。):
#include <boost/function_types/result_type.hpp>
#include <boost/type_traits/is_same.hpp>
template<typename F, typename R>
struct is_result_same :
boost::is_same<
typename boost::function_types::result_type<F>::type,
R
>
{};
template<class F, bool = is_result_same<F, void>::value>
struct special {};
template<class R, class C> struct special<R(C::*)(), true> {};
template<class R, class C> struct special<R(C::*)(), false> {};
我想這是因爲當解決R爲'void'的情況時,兩種專業化的簽名實際上是相同的......一些聰明的'typename'關鍵字的使用可能會有幫助,但我現在無法想到任何聰明的東西。 – AJG85 2011-02-24 22:02:01