Reputation: 65
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?
Upvotes: 1
Views: 1205
Reputation: 65
Yes, your idea would work. I rewrote it slighly. However, what I am wondering is if there is a way to do this same thing with an inplace functor, using bind and lambda. In the mean time, I'll use your idea.
template <typename type_t> struct static_assign { type_t* a; static_assign(type_t* a) : a(a) {} template<typename T> void operator()(T t) { *a++ = t; } }; uint32_t color[MAX_COLORS]; uint32_t* pcolor = (uint32_t*)&color; boost::mpl::for_each<typename static_color<scheme_t>::type>(static_assign<uint32_t>(pcolor)); pcolor += indicator;
Upvotes: 0
Reputation: 12901
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));
Upvotes: 2