fork download
  1. #include <iostream>
  2. #include <ostream>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. template<typename T,unsigned size>
  8. struct Array
  9. {
  10. typedef T type[size];
  11. mutable type data;
  12. Array()
  13. {
  14. cout << "Array::Array" << endl;
  15. }
  16. ~Array()
  17. {
  18. cout << "Array::~Array" << endl;
  19. }
  20. };
  21.  
  22. template<typename T> inline
  23. typename Array<T,1>::type &make_array(const T &p1,const Array<T,1> &aux=Array<T,1>())
  24. {
  25. aux.data[0]=p1;
  26. return aux.data;
  27. }
  28.  
  29. template<typename T> inline
  30. typename Array<T,2>::type &make_array(const T &p1,const T &p2,const Array<T,2> &aux=Array<T,2>())
  31. {
  32. aux.data[0]=p1;
  33. aux.data[1]=p2;
  34. return aux.data;
  35. }
  36.  
  37. template<typename T> inline
  38. typename Array<T,3>::type &make_array(const T &p1,const T &p2,const T &p3,const Array<T,3> &aux=Array<T,3>())
  39. {
  40. aux.data[0]=p1;
  41. aux.data[1]=p2;
  42. aux.data[2]=p3;
  43. return aux.data;
  44. }
  45.  
  46. // ...
  47.  
  48. void test_array(int (&p)[3])
  49. {
  50. cout << p[0] << " " << p[1] << " " << p[2] << endl;
  51. }
  52.  
  53. void test_ptr(int *p)
  54. {
  55. cout << p[0] << " " << p[1] << " " << p[2] << endl;
  56. }
  57.  
  58. int main(int argc,char *argv[])
  59. {
  60. test_array(make_array(33,22,11));
  61. test_ptr(make_array(33,22,11));
  62. return 0;
  63. }
  64.  
Success #stdin #stdout 0.01s 2680KB
stdin
Standard input is empty
stdout
Array::Array
33 22 11
Array::~Array
Array::Array
33 22 11
Array::~Array