首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模板类声明-不是常量表达式。

模板类声明-不是常量表达式。
EN

Stack Overflow用户
提问于 2014-02-11 05:15:47
回答 1查看 264关注 0票数 0

我有下面的类,声明时需要有两个常量。

代码语言:javascript
复制
template <int PAGE_DIV_SIZE, int BUFFERS_NUM>
    class BufferPool {
    //...
    }

下面是它的测试代码

代码语言:javascript
复制
void testBufferPool(const int pageDivSize, const int bufferNum){   

    // other code and declaration

    BufferPool <pageDivSize, bufferNum> bufferPool(catalog, devNum, hostCapacityVec, devCapacityVec);


} 

我得到以下错误:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-11 05:26:50

为了实例化模板,编译器必须在编译时知道所有模板参数。无法在编译时计算出pageDivSize和bufferNum的值。因此,模板参数不应该是常量变量,而应该是常量表达式。

expression

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21693696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档