我有下面的类,声明时需要有两个常量。
template <int PAGE_DIV_SIZE, int BUFFERS_NUM>
class BufferPool {
//...
}下面是它的测试代码
void testBufferPool(const int pageDivSize, const int bufferNum){
// other code and declaration
BufferPool <pageDivSize, bufferNum> bufferPool(catalog, devNum, hostCapacityVec, devCapacityVec);
} 我得到以下错误:
error: ‘pageDivSize’ is not a constant expression
BufferPoolTest.cpp:26:39: note: in template argument for type ‘int’
BufferPoolTest.cpp:26:39: error: ‘bufferNum’ is not a constant expression
BufferPoolTest.cpp:26:39: note: in template argument for type ‘int’
BufferPoolTest.cpp:26:51: error: invalid type in declaration before ‘(’ token
BufferPoolTest.cpp:26:100: error: expression list treated as compound expression in initializer [-fpermissive]
BufferPoolTest.cpp:26:100: error: cannot convert ‘std::vector<long unsigned int>’ to ‘int’ in initialization发布于 2014-02-11 05:26:50
为了实例化模板,编译器必须在编译时知道所有模板参数。无法在编译时计算出pageDivSize和bufferNum的值。因此,模板参数不应该是常量变量,而应该是常量表达式。
expression
https://stackoverflow.com/questions/21693696
复制相似问题