fork download
  1. #include <iostream>
  2. #include <functional>
  3. #include <sstream>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. int f1(int x) {
  8. return x;
  9. }
  10.  
  11. string f2(int x) {
  12. return std::to_string(x);
  13. }
  14.  
  15. long long f3(int x) {
  16. return x;
  17. }
  18.  
  19. template<class T>
  20. function<string(int)> conv(function<T(int)> f) {
  21. return [&](int x) -> string {
  22. stringstream ss;
  23. ss << f(x);
  24. return ss.str();
  25. };
  26. }
  27.  
  28. template<class T>
  29. function<string(int)> conv1(T(*f)(int)) {
  30. return [f](int x) -> string {
  31. stringstream ss;
  32. std::function<T(int)> _f(f);
  33. ss << _f(x);
  34. return ss.str();
  35. };
  36. }
  37.  
  38. int main() {
  39.  
  40. vector<function<string(int)>> funcs = {
  41. conv(std::function<int(int)>(f1)),
  42. conv1(f1),
  43. conv(std::function<string(int)>(f2)),
  44. conv1(f2),
  45. conv(std::function<long long(int)>(f3)),
  46. conv1(f3),
  47. };
  48. std::cout << "int(int) " << funcs[0](12) << " " << funcs[1](13) << std::endl;
  49. std::cout << "string(int) "<< funcs[2](13) << " " << funcs[2](12) << std::endl;
  50. std::cout << "long long(int) "<< funcs[4](12) << " " << funcs[5](13) << std::endl;
  51. return 0;
  52. }
  53.  
Runtime error #stdin #stdout 0s 16080KB
stdin
Standard input is empty
stdout
int(int) 12 13