fork(1) 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 [f](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. ss << f(x);
  33. return ss.str();
  34. };
  35. }
  36.  
  37. int main() {
  38.  
  39. vector<function<string(int)>> funcs = {
  40. conv(std::function<int(int)>(f1)),
  41. conv1(f1),
  42. conv(std::function<string(int)>(f2)),
  43. conv1(f2),
  44. conv(std::function<long long(int)>(f3)),
  45. conv1(f3),
  46. };
  47. std::cout << "int(int) " << funcs[0](12) << " " << funcs[1](13) << std::endl;
  48. std::cout << "string(int) "<< funcs[2](12) << " " << funcs[3](13) << std::endl;
  49. std::cout << "long long(int) "<< funcs[4](12) << " " << funcs[5](13) << std::endl;
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0s 15248KB
stdin
Standard input is empty
stdout
int(int) 12 13
string(int) 12 13
long long(int) 12 13