2010-12-01 48 views
1

我有四個功能:曖昧的函數的調用

template<class Exception,class Argument> 
void allocate_help(const Argument& arg,Int2Type<true>)const; 

template<class Exception,class Argument> 
std::nullptr_t allocate_help(const Argument& arg,Int2Type<false>)const; 

template<class Exception> 
void allocate_help(const Exception& ex,Int2Type<true>)const; 

template<class Exception> 
std::nullptr_t allocate_help(const Exception& ex,Int2Type<false>)const; 

但是當我打電話:

allocate_help<std::bad_alloc>(e,Int2Type<true>()); //here e is of a std::bad_alloc type 

我得到一個錯誤:
錯誤3錯誤C2668:曖昧調用超載功能 爲什麼?

回答

2

因爲您的來電都匹配:

template<class Exception,class Argument> 
void allocate_help(const Argument& arg,Int2Type<true>)const; 

Exception = std::bad_allocArgument = std::bad_allocArgument自動推導出),並且:

template<class Exception> 
void allocate_help(const Exception& ex,Int2Type<true>)const; 

Exception = std::bad_alloc。因此呼叫的含糊不清。

此外,我認爲你的編譯器應該輸出錯誤行之後的所有匹配功能,所以你可以自己回答你的問題。

2

因爲他們是ambigious。

template<class Exception,class Argument> 
std::nullptr_t allocate_help(const Argument& arg,Int2Type<true>)const; 

template<class Exception> 
std::nullptr_t allocate_help(const Exception& ex,Int2Type<true>)const; 

所述第二函數的簽名是第一個,這意味着,在函數調用的上下文它們是相同的以「任何類型」作爲第一個參數,Int2Type作爲第二個參數的子集。

allocate_help<std::bad_alloc>(e,Int2Type<true>()); 

能成爲可以:

std::nullptr_t allocate_help<std::bad_alloc, std::bad_alloc>(const std::bad_alloc& arg,Int2Type<true>)const; 

std::nullptr_t allocate_help<std::bad_alloc>(const std::bad_alloc& ex,Int2Type<true>)const; 

如何將編譯器選擇?

+1

+1,但他們沒有完全相同的簽名,第二個版本是第一個,在類型`Exception`和`Argument`是相同的一個子集。模板參數「Exception」的任何調用和函數調用的第一個參數的類型都不會是模糊的。 – 2010-12-01 17:08:43