fork download
  1. #include <iostream>
  2.  
  3. template < size_t N, typename T >
  4. struct param_packer : param_packer<N-1,T>
  5. {
  6. int value;
  7.  
  8. param_packer( param_packer<N-1,T> const & other,
  9. int last )
  10. : param_packer<N-1,T>(other), value(last)
  11. { }
  12.  
  13. param_packer<N+1,T>
  14. operator,( T append ) const {
  15. return param_packer<N+1,T>( *this, append );
  16. }
  17. };
  18.  
  19. template < typename T >
  20. struct param_packer<0,T>
  21. {
  22. param_packer<1,T>
  23. operator,( T first ) const {
  24. return param_packer<1,T>( *this, first );
  25. }
  26. };
  27.  
  28. template < size_t I, size_t N, typename T >
  29. T&
  30. param_at( param_packer<N,T> & pack ) {
  31. return static_cast< param_packer<I+1,T>* >(&pack)->value;
  32. }
  33.  
  34. param_packer<0,int> _;
  35.  
  36. template < size_t N >
  37. void foo( param_packer<N, int> pack ) {
  38. std::cout << "N = " << N << std::endl;
  39. std::cout << "\t param at <0> is: " << param_at<0>( pack ) << std::endl;
  40. std::cout << "\t param at <N-1> is: " << param_at<N-1>( pack ) << std::endl;
  41. }
  42.  
  43. int main() {
  44.  
  45. foo( (_, 1, 2) );
  46.  
  47. }
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
N = 2
	 param at <0> is: 1
	 param at <N-1> is: 2