Constexpr,模板和数组大小

时间:2013-07-06 20:27:51

标签: c++ templates c++11

我想将模板参数传递给函数调用,返回值用作数组的大小,即

constexpr int myPow(int a, int b){
  int result = 1;
  for(int i=0;i<b;i++)
    result *= a;
  return result;
}

template <int N>
class testClass{
public:
  testClass(){}
  int array[myPow(2,N)];
};

int main(){
  testClass<3> A;
  return 0;
}

编译错误:

~ $ g++-4.6 test.cpp -std=gnu++0x
test.cpp: In function ‘constexpr int myPow(int, int)’:
test.cpp:6:1: error: body of constexpr function ‘constexpr int myPow(int, int)’ not a return-statement
test.cpp: At global scope:
test.cpp:12:23: error: array bound is not an integer constant before ‘]’ token

知道怎么解决这个问题吗?

3 个答案:

答案 0 :(得分:7)

在C ++ 11中,constexpr函数只能包含return语句(有关完整详细信息,请参阅here),因此myPow函数不是constexpr -compliant(因为它包含一个for循环)。

您可以使用此元函数在编译时计算整数幂:

template <int N, typename Type> 
constexpr Type pow(const Type& x) 
{
    return (N > 1) ? (x*pow<(N-1)*(N > 1)>(x)) 
                   : ((N < 0) ? (static_cast<Type>(1)/pow<(-N)*(N < 0)>(x)) 
                              : ((N == 1) ? (x) 
                                          : (static_cast<Type>(1))));
}

如果您要计算2^N,可以输入:

pow<N>(2)

注1:此元函数非常通用,也适用于负整数和浮点类型,因此您可以键入:pow<-3>(3.14)

注2:模板中的N>1N<0乘法在此处阻止无限递归,并在分支不相关时强制模板参数等于零。这可以通过模板特化来完成,但这里使用的技术允许编写单个函数。

答案 1 :(得分:7)

在C ++ 11中,constexpr函数受到很大限制,并且您的代码不符合限制(您不能声明变量,改变本地状态,也不能使用大多数形式的语句 - 包括循环)。但是,C ++ 1y删除了大多数限制,而Clang 3.3接受了-std=c++1y模式下的原始代码示例。

如果您需要代码在C ++ 11模式下工作,您可以重写它以回避constexpr限制:

constexpr int myPow(int a, int b) {
  return b ? a * myPow(a, b - 1) : 1;
}

答案 2 :(得分:2)

编辑:转到理查德史密斯的更聪明的答案。

根据您接受的答案,没有必要使用元函数, 将myPow算法实现为constexpr - 限定函数。

您可以默认指数参数= 0然后:

constexpr int myPow(int a, int b = 0){
    return b == 0 ? 1 : myPow(a,b - 1) * a;
}

如果你不喜欢默认那个论点,那么你也可以 默认情况下,constexpr辅助中的那个参数只有myPow 调用,例如

namespace impl {
    constexpr int myPow(int a, int b = 0){
        return b == 0 ? 1 : myPow(a,b - 1) * a;
    }
}
constexpr int myPow(int a, int b){
    return impl::myPow(a,b);
}

如果您至少升级到gcc 4.7.2,您可以使用-std=c++11 甚至隐藏在myPow本身内的辅助,因为你将被允许 在constexpr函数体内定义类型:

constexpr int myPow(int a, int b){
    struct _1 {
        static constexpr int _2(int a, int b = 0) {
            return b == 0 ? 1 : _2(a,b - 1) * a;
        }
    };
    return _1::_2(a,b);
}

(虽然我认为严格来说这个纬度是C ++ 1y扩展名。)

你可能想要改编Vincent的优越算法 它不再是N中的元函数,但仍然是通用的 算术类型。