tags:

views:

291

answers:

2

Using boost::mpl, I can create a typedef of a three element vector like follows:

typedef boost::mpl::vector_c<int,1,2,3> height_t;

I can pull the values out of this typedef with the following snippit:

std::vector<int> height;
boost::mpl::for_each<height_t>(boost::bind(&std::vector<int>::push_back, &height, _1));   

assert(height[0] == 1);
assert(height[1] == 2);
assert(height[2] == 3);

I'm wondering if there is a way to do this same thing but with a normal 'C' array instead of a std::vector. Unfortunately, I can't use STL containers in this project.

uint32_t height[3];
boost::mpl::for_each<height_t>(????, &height, _1));    

I suspect that I need to replace the ???? with another bind clause. Any ideas?

+2  A: 

Try something like

 struct foo { 
     int c;
     int* arr;
     template<class U> 
     void operator(U& u) { arr[c++] = u; }
     foo(int a*) : c(0), arr(a) {}
 };

 for_each<height_t>(foo(height));
KitsuneYMG
Taking the array by reference would be better then letting it decay to a pointer, e.g. `template<int n> struct foo { int ( foo(int (`
Georg Fritzsche
A: 
Tom Brinkman