问问题
3542 次
2 回答
3
Using a macro function is the wrong tool for the job. Nevertheless, you say you think it would work inside expressions, consider how would that look for the actual compiler once the preprocessor did his work:
int result = int c=2; for (int ii=0; ii<(10)-1; (c)*=(2), i++); c
No matter how hard you try, you can't do variable definitions nor for
loops within an expression.
于 2012-06-08T17:47:10.550 回答
3
You could use a recursive template template approach:
template<int base, unsigned int exp>
struct Pow {
enum { value = base * power<base, exp-1>::value };
};
// stopping condition
template<int base>
struct Pow<base,0> {
enum { value = 1 };
};
and use it like this:
int i = Pow<10,2>::value;
于 2012-06-08T18:05:04.610 回答