I am not able to figure out why following code does not compile. In the overload 1, the type deduced is int &
so, it should result in SFINAE. And we should only have one candidate function.
#include <iostream>
#include <type_traits>
template <typename T>
std::enable_if<std::is_same<T, std::remove_reference_t<T>>::value, void> foo(T&& a){
std::cout << "overload 1" << std::endl;
}
template <typename T>
void foo(T&& 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, int&>::value << std::endl; ---> gives false
return 0;
}
Compilation Error:
template_with_reference.cpp: In function ‘int main()’:
template_with_reference.cpp:19:8: error: call of overloaded ‘foo(int&)’ 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(T&&) [with T = int&; 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(T&& a){
^~~
template_with_reference.cpp:11:6: note: candidate: void foo(T&&) [with T = int&]
Could you guys help me find the error?
Aucun commentaire:
Enregistrer un commentaire