fork download
  1. #include <cstdio>
  2. #include <memory>
  3.  
  4. template <typename T>
  5. struct allocator{
  6. template<typename... Args>
  7. auto operator()(Args&&... args) const {
  8. return std::make_unique<T>(std::forward<Args>(args)...);
  9. }
  10. };
  11.  
  12. template <typename T, typename A = allocator<T>>
  13. auto get(A a=A{}) {
  14. return [a](auto... args){
  15. return a(args...);
  16. };
  17. };
  18.  
  19.  
  20. int main() {
  21. auto up0 = get<int>()();
  22. auto up1 = get<int>()(1);
  23. auto up0b = get<int>(allocator<int>())();
  24. auto up1b = get<int>(allocator<int>())(1);
  25. auto up0c = get<int>([](auto ... args){ return std::make_unique<int>(args...); })();
  26. auto up1c = get<int>([](auto ... args){ return std::make_unique<int>(args...); })(1);
  27.  
  28. printf("%d\n", *up0);
  29. printf("%d\n", *up0b);
  30. printf("%d\n", *up0c);
  31. printf("%d\n", *up1);
  32. printf("%d\n", *up1b);
  33. printf("%d\n", *up1c);
  34. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
0
0
0
1
1
1