您可以BOOST_PP_ENUM
用作:
#include <iostream>
#include <boost/preprocessor/repetition/enum.hpp>
#define MODER 10
template<int a, int n> struct pow
{
static const int value = a * pow<a, n-1>::value % MODER;
};
template<int a> struct pow<a, 0>
{
static const int value = 1;
};
#define ORDER(count, i, data) pow<data,i>::value
int main() {
const int p = 3;
int const a[] = { BOOST_PP_ENUM(10, ORDER, p) };
std::size_t const n = sizeof(a)/sizeof(int);
for(std::size_t i = 0 ; i != n ; ++i )
std::cout << a[i] << "\n";
return 0;
}
输出:
1
3
9
7
1
3
9
7
1
3
见在线演示
该行:
int const a[] = { BOOST_PP_ENUM(10, ORDER, p) };
扩展为:
int const a[] = { pow<p,0>::value, pow<p,1>::value, ...., pow<p,9>::value};