fork download
  1. #include <cstdlib>
  2. #include <utility>
  3.  
  4. template <typename T>
  5. struct new_impl
  6. {
  7. typedef T* type;
  8.  
  9. static T* call() { return new T; }
  10. };
  11.  
  12. template <typename T>
  13. struct new_impl<T[]>
  14. {
  15. typedef T* type;
  16.  
  17. static T* call(std::size_t i) { return new T[i]; }
  18. };
  19.  
  20. template <typename T, std::size_t N>
  21. struct new_impl<T[N]>
  22. {
  23. typedef T* type;
  24.  
  25. static T* call() { return new T[N]; }
  26. };
  27.  
  28. template <typename T, typename ...Args>
  29. typename new_impl<T>::type new_(Args&&... args) {
  30. return new_impl<T>::call(std::forward<Args>(args)...);
  31. }
  32.  
  33. int main()
  34. {
  35. int* t1 = new_<int>();
  36. int* t2 = new_<int[]>(20);
  37. int* t3 = new_<int[20]>();
  38.  
  39. return 0;
  40. }
  41.  
  42.  
Success #stdin #stdout 0s 3224KB
stdin
Standard input is empty
stdout
Standard output is empty