Ambiguous call to a function

I have four functions:

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;

but when I call:

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

I'm getting an error:
Error 3 error C2668: ambiguous call to overloaded function Why?

Upvotes: 1

Views: 529

Answers (2)

ronag
ronag

Reputation: 51293

Because they are 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;

The signature of the second function is a subset of the first one, which means that in the context of your function call they are the same of taking "any type" as first argument and Int2Type as second argument.

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

Can become either:

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

or

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

How would the compiler choose?

Upvotes: 2

Yakov Galka
Yakov Galka

Reputation: 72539

Because your call matches both:

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

with Exception = std::bad_alloc and Argument = std::bad_alloc (Argument is automatically deduced), and:

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

with Exception = std::bad_alloc. Hence the ambiguity of the call.

Also I think that your compiler should output all the matching function after the error line, so you could answer your question yourself.

Upvotes: 2

Related Questions