Bonjour,
Je cherche � faire marcher un code du genre :
Avec ensuite :
Code : S�lectionner tout - Visualiser dans une fen�tre � part
1
2
3
4
5
6 template<typename... args> int func(std::function<int(args...)> f) { return sizeof...(args); // juste pour tester }
Le code suivant compile bien :
Code : S�lectionner tout - Visualiser dans une fen�tre � part
1
2
3
4
5 int f2(int a, int b, int c) { return a + b + c; }
En revanche, celui-ci ne compile pas :
Code : S�lectionner tout - Visualiser dans une fen�tre � part
1
2
3 std::function<int(int, int, int)> f3 = f2; func(f3); // renvoie bien 3
Et celui-ci non plus :
Code : S�lectionner tout - Visualiser dans une fen�tre � part
1
2
3
4
5
6 func(f2); /* error: no matching function for call to func(int (&)(int, int, int)) test.cpp:45:25: note: candidate is: test.cpp:28:5: note: template<class ... args> int func(std::function<int(args ...)>) test.cpp:28:5: note: template argument deduction/substitution failed: test.cpp:45:25: note: mismatched types std::function<int(args ...)> and int (*)(int, int, int) */
Pour le premier cas, je peux fournir un overload :
Code : S�lectionner tout - Visualiser dans une fen�tre � part
1
2
3
4
5
6 func([](int a, int b, int c) { return f2(a,b,c); }); /* error: no matching function for call to func(main()::<lambda(int, int, int)>) test.cpp:44:68: note: candidate is: test.cpp:28:5: note: template<class ... args> int func(std::function<int(args ...)>) test.cpp:28:5: note: template argument deduction/substitution failed: test.cpp:44:68: note: main()::<lambda(int, int, int)> is not derived from std::function<int(args ...)> */
Pas tr�s �l�gant, mais �a marche. En revanche, pour le deuxi�me cas, je ne vois pas comment faire sans fournir l�ensemble des surcharges jusqu�� n arguments, ce qui est justement ce que je souhaitais �viter avec les templates variadic.
Code : S�lectionner tout - Visualiser dans une fen�tre � part
1
2
3
4
5
6 template<typename... args> int func(int (*f)(args...)) { return sizeof...(args); }
Mon compilateur est gcc 4.7.2, je ne peux pas en changer. Des suggestions ?
Partager