Неоднозначный вызов функции - PullRequest
1 голос
/ 01 декабря 2010

У меня четыре функции:

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 ]

2 голосов
/ 01 декабря 2010

Поскольку они неоднозначны.

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;

Как будет выбирать компилятор?

2 голосов
/ 01 декабря 2010

Поскольку ваш вызов соответствует как:

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

с Exception = std::bad_alloc и Argument = std::bad_alloc (автоматически выводится Argument), так и:

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

с Exception = std::bad_alloc,Отсюда и неоднозначность вызова.

Также я думаю, что ваш компилятор должен выводить все функции соответствия после строки ошибки, чтобы вы могли ответить на свой вопрос самостоятельно.

...