Dato il seguente codice: -sovraccarico sui std :: funzione <...>
#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;
}
errore di compilazione da 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>)'
errore di compilazione da 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)>)
Così sembra che il compilatore non riesca a capire che un lambda []() -> void può essere assegnato solo a una std :: function < void (void) > e un lambda [] (int) -> void può essere assegnato solo a una std :: function < void (int) >. Questo dovrebbe succedere o solo una carenza nei compilatori?
Dimmi se ho capito bene. Questo snippet ha lo stesso problema per lo stesso motivo (non è possibile istanziare Struct in tempo per determinare il sovraccarico della funzione)? 'modello struct Struct {Struct (T var) {}}; vuoto func2 (struct obj) {} vuoto func2 (struct obj) {} int main() {func2 (2.5); } ' –
Arnavion
No, è un po 'diverso da quello. Questo errore è dovuto al fatto che 'T' si trova in un contesto non condotto. Modificherò una spiegazione nella mia risposta; questo commento non è abbastanza grande. –
Capito. Grazie! – Arnavion