这是剩下的东西,使用 boost fusion 容器,特别是map。
#include <iostream>
#include <boost/fusion/container/map.hpp>
#include <boost/fusion/sequence/intrinsic/at_key.hpp>
#include <boost/array.hpp>
struct A{int a, b, c;};
struct B{double a, b, c;};
struct C{bool a, b, c; };
template <class T>
struct data
{
data() : buffer() {}
size_t size() const { return buffer.size(); }
boost::array<char, sizeof(T)> buffer; // assuming no alignment issues!
};
int main(void)
{
boost::fusion::map<boost::fusion::pair<A, data<A> >,
boost::fusion::pair<B, data<B> >,
boost::fusion::pair<C, data<C> >
> buffer_holder;
// to access
std::cout << boost::fusion::at_key<A>(buffer_holder).size() << std::endl; // returns reference to data<A>
return 0;
}
在这里,每种类型的所有缓冲区都归一个组件所有,并且每个缓冲区都经过正确构造并且可以由data
包装器管理(如果您在多线程环境中很有用)。不是静态的...缺点是您可以使用的模板参数的数量是有限的,您可以使用编译器标志来增加它(我之前使用了多达 30 个)。