Dado el siguiente código: -Sobrecarga en std :: función <...>
#include <algorithm>
#include <iostream>
#include <functional>
#include <string>
void func(std::function<void(void)> param)
{
param();
}
void func(std::function<void(int)> param)
{
param(5);
}
int main(int argc, char* argv[])
{
func([]() { std::cout << "void(void)" << std::endl; });
func([] (int i) { std::cout << "void(int): " << i << std::endl; });
std::string line;
std::getline(std::cin, line);
return 0;
}
error de compilación de VS2010: -
CppTest.cpp(18): error C2668: 'func' : ambiguous call to overloaded function
1> CppTest.cpp(11): could be 'void func(std::tr1::function<_Fty>)'
1> with
1> [
1> _Fty=void (int)
1> ]
1> CppTest.cpp(6): or 'void func(std::tr1::function<_Fty>)'
1> with
1> [
1> _Fty=void (void)
1> ]
1> while trying to match the argument list '(`anonymous-namespace'::<lambda0>)'
1>CppTest.cpp(19): error C2668: 'func' : ambiguous call to overloaded function
1> CppTest.cpp(11): could be 'void func(std::tr1::function<_Fty>)'
1> with
1> [
1> _Fty=void (int)
1> ]
1> CppTest.cpp(6): or 'void func(std::tr1::function<_Fty>)'
1> with
1> [
1> _Fty=void (void)
1> ]
1> while trying to match the argument list '(`anonymous-namespace'::<lambda1>)'
error de compilación de g ++ - 4,5
program2.cpp: In function ‘int main(int, char**)’:
program2.cpp:18:68: error: call of overloaded ‘func(main(int, char**)::<lambda()>)’ is ambiguous
program2.cpp:6:10: note: candidates are: void func(std::function<void()>)
program2.cpp:11:10: note: void func(std::function<void(int)>)
program2.cpp:19:79: error: call of overloaded ‘func(main(int, char**)::<lambda(int)>)’ is ambiguous
program2.cpp:6:10: note: candidates are: void func(std::function<void()>)
program2.cpp:11:10: note: void func(std::function<void(int)>)
Así parece que el compilador no puede entender que un lambda []() -> void solo se puede asignar a una función std :: < void (void) >, y un lambda [] (int) -> void solo se puede asignar a std :: function < void (int) >. ¿Se supone que esto suceda o solo una deficiencia en los compiladores?
Dime si tengo este derecho. Este fragmento tiene el mismo problema por la misma razón (¿no se puede instanciar Struct a tiempo para determinar la sobrecarga de func)? 'plantilla struct Struct {Struct (T var) {}}; void func2 (Struct obj) {} void func2 (Struct obj) {} int main() {func2 (2,5); } ' –
Arnavion
No, es un poco diferente a eso. Ese error es porque 'T' está en un contexto no deducido. Editaré una explicación en mi respuesta; este comentario no es lo suficientemente grande. –
Entendido. ¡Gracias! – Arnavion