#c #c 11 #templates #typetraits
#c #c 11 #шаблоны #типовые признаки
Вопрос:
Я не могу понять, почему следующий код не компилируется. В перегрузке 1 выводится тип int amp;
so, это должно привести к SFINAE . И у нас должна быть только одна функция-кандидат.
#include <iostream>
#include <type_traits>
template <typename T>
std::enable_if<std::is_same<T, std::remove_reference_t<T>>::value, void> foo(Tamp;amp; a){
std::cout << "overload 1" << std::endl;
}
template <typename T>
void foo(Tamp;amp; a){
std::cout << std::is_same<T, std::remove_reference_t<T>>::value << std::endl;
std::cout << "overload 2" << std::endl;
}
int main(){
int a = 2;
foo(a);
// std::cout << std::is_same<int, intamp;>::value << std::endl; ---> gives false
return 0;
}
Ошибка компиляции:
template_with_reference.cpp: In function ‘int main()’:
template_with_reference.cpp:19:8: error: call of overloaded ‘foo(intamp;)’ is ambiguous
foo(a);
^
template_with_reference.cpp:5:74: note: candidate: std::enable_if<std::is_same<T, typename std::remove_reference< <template-parameter-1-1> >::type>::value, void> foo(Tamp;amp;) [with T = intamp;; typename std::remove_reference< <template-parameter-1-1> >::type = int]
std::enable_if<std::is_same<T, std::remove_reference_t<T>>::value, void> foo(Tamp;amp; a){
^~~
template_with_reference.cpp:11:6: note: candidate: void foo(Tamp;amp;) [with T = intamp;]
Не могли бы вы, ребята, помочь мне найти ошибку?
Комментарии:
1. Потому что вы использовали
enable_if
вместоenable_if_t
.2. О, как же я этого не заметил. @Holt Большое вам спасибо за такой быстрый ответ.