fork download
  1. #include <iostream>
  2. #include <ostream>
  3. using namespace std;
  4.  
  5. // ______________________________________________________________
  6.  
  7. template<typename T,unsigned size>
  8. struct Array
  9. {
  10. typedef T type[size];
  11. mutable type values;
  12. Array()
  13. {
  14. cout << "Array::Array" << endl;
  15. }
  16. ~Array()
  17. {
  18. cout << "Array::~Array" << endl;
  19. }
  20. };
  21.  
  22. #include <boost/preprocessor/iteration/local.hpp>
  23. #include <boost/preprocessor/repetition/enum.hpp>
  24. #include <boost/preprocessor/repetition/repeat.hpp>
  25.  
  26. #define MAKE_ARRAY_PP_MAX_ARG_COUNT 16
  27.  
  28. #define MAKE_ARRAY_PP_PARAM_LIST(z, n, data) const T & BOOST_PP_CAT(p, n)
  29. #define MAKE_ARRAY_PP_PARAM_ASSIGN(z, n, data) aux.values[n] = BOOST_PP_CAT(p, n);
  30.  
  31. #define BOOST_PP_LOCAL_MACRO(n) \
  32. template<typename T> inline \
  33. typename Array<T,n>::type &make_array(BOOST_PP_ENUM(n, MAKE_ARRAY_PP_PARAM_LIST, _) , const Array<T,n> &aux=Array<T,n>()) \
  34. { \
  35.   BOOST_PP_REPEAT(n, MAKE_ARRAY_PP_PARAM_ASSIGN, _) \
  36.   return aux.values; \
  37. } \
  38. /**/
  39. #define BOOST_PP_LOCAL_LIMITS (1, MAKE_ARRAY_PP_MAX_ARG_COUNT)
  40. #include BOOST_PP_LOCAL_ITERATE()
  41.  
  42. // ______________________________________________________________
  43.  
  44. void test_array(int (&p)[3])
  45. {
  46. cout << p[0] << " " << p[1] << " " << p[2] << endl;
  47. }
  48.  
  49. void test_ptr(int *p)
  50. {
  51. cout << p[0] << " " << p[1] << " " << p[2] << endl;
  52. }
  53.  
  54. int main(int argc,char *argv[])
  55. {
  56. test_array(make_array(33,22,11));
  57. test_ptr(make_array(33,22,11));
  58. make_array(33,22,11,00,55,44,66);
  59. make_array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
  60. return 0;
  61. }
  62.  
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
Array::Array
Array::~Array
Array::Array
Array::~Array