在C编译时没有内置的计算能力的方法吗?

我有以下非常简单的模板.据我所知,^不是指数运算符.现在我正在寻找一种计算这种能力的方法.互联网上有一个递归模板的例子.这不是太难了.

但是我想知道:C中是否在编译时实际上没有“内置”方法来计算?

template <int DIM>
class BinIdx : Idx
{
        static const int SIZE = 3 ^ DIM; // whoops,this is NOT an exponential operator!
}

解决方法

您可以使用模板元编程.让我显示代码.
template <int A,int B>
struct get_power
{
    static const int value = A * get_power<A,B - 1>::value;
};
template <int A>
struct get_power<A,0>
{
    static const int value = 1;
};

用法

std::cout << get_power<3,3>::value << std::endl;

(live example)

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...