1

使用 boost::mpl,我可以创建一个三元素向量的 typedef,如下所示:

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

我可以使用以下代码段从这个 typedef 中提取值:

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);

我想知道是否有办法做同样的事情,但使用普通的 'C' 数组而不是std::vector. 不幸的是,我不能在这个项目中使用 STL 容器。

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

我怀疑我需要更换????与另一个绑定子句。有任何想法吗?

4

2 回答 2

2

尝试类似的东西

 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));
于 2010-01-01T03:41:09.257 回答
0

是的,你的想法会奏效。我稍微重写了一遍。但是,我想知道是否有一种方法可以使用 inplace 仿函数,使用 bind 和 lambda 来做同样的事情。同时,我会使用你的想法。

模板 <typename type_t>
结构静态分配
{
    类型_t*一个;
    static_assign(type_t* a) : a(a) {}

    模板<类型名 T>
    无效运算符()(T t)
    {
        *a++ = t;
    }
};

uint32_t 颜色[MAX_COLORS];
uint32_t* pcolor = (uint32_t*)&color;
boost::mpl::for_each<typename static_color<scheme_t>::type>(static_assign<uint32_t>(pcolor));
pcolor += 指标;

于 2010-01-03T02:23:40.083 回答