boost::mpl::vector
作为阶乘函数的查找表,这是一个更通用库函数的测试,开发人员可以使用该函数生成原语静态数组形式的查找表。该函数(最有可能被实现为预处理器宏定义)将接受要初始化的数组的名称和大小,以及要用作元函数初始化每个元素 i 的类模板的名称。我认为在不使用外部脚本的情况下做到这一点的最佳方法是:
- 创建一个
boost::mpl::vector
,如下面的代码清单中所示,并将用户提供的元函数的返回值推送到向量的后面,以初始化数组的每个元素; - 使用向量的元素初始化静态数组(也许通过使用一系列宏,其中最后一个宏将使用
__VARARGS__
宏来实现这一点)。
Is my procedure a good way of accomplishing what I seek? If not, please describe a better procedure which would accomplish the same thing, without the use of external scripts.
If my procedure is indeed a good way of accomplishing what I seek, how would I implement (2)?
I will be sure to post a link to the source file containing library function which I describe once I implement it. The code listing follows below.
namespace mpl = boost::mpl;
template <typename x> struct factorial: mpl::if_<mpl::greater<x, mpl::int_<1>>, mpl::multiplies<x, factorial<x::prior>>, mpl::int_<1> >::type {}; template <typename sequence, typename size> struct compileTable: mpl::if_<mpl::greater<size, mpl::int_<0>>, compileTable< mpl::push_front<sequence, factorial<size>>::type, size::prior >, sequence >::type {}; static const int TABLE_SIZE = 13; typedef compileTable< mpl::vector<>, mpl::int_<TABLE_SIZE> >::type factorialTable; /* ** This is where I am stuck; how would I use the elements ** of factorialTable to initialize a static array? */
static int foo[]
是指什么? - Anycorn