首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模板变量“列表”不起作用?

模板变量“列表”不起作用?
EN

Stack Overflow用户
提问于 2013-07-27 16:51:56
回答 2查看 79关注 0票数 3

我有以下模板类:

代码语言:javascript
复制
template <class T, list<int> t> 
class Tops
{
private:
    list<stack<T>> tops;
public:
    list getTops() {

    }
};

它不编译为:illegal type for non-type template parameter 't'

在第6行:template <class T, list<int> t> class Tops

当我将list<int>更改为int类型时,它可以工作。有什么问题吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-27 17:05:00

template <class T, list<int> t>更改为template <class T, list<int> &t>

代码语言:javascript
复制
template <class T, list<int> &t>   
                             ^^^  //note &t
class Tops
{
private:
    list<stack<T>> tops;
public:
    list getTops() {

    }
};

您不能这样做的原因是非常数表达式不能在编译时被解析和替换。它们可能在运行时进行更改,这将需要在运行时生成新模板,这是不可能的,因为模板是编译时的概念。

这里是标准允许的非类型模板参数(14.1 temp.param p4):

代码语言:javascript
复制
A non-type template-parameter shall have one of the following 
(optionally cv-qualified) types:

 - integral or enumeration type,
 - pointer to object or pointer to function,
 - reference to object or reference to function,,
 - pointer to member.

资料来源:https://stackoverflow.com/a/5687553/1906361

票数 0
EN

Stack Overflow用户

发布于 2013-07-27 17:02:04

在编译时解析template的参数。

模板参数必须是常量表达式、函数/对象/静态成员的地址或对象的引用。

我想你是在找这个:

代码语言:javascript
复制
template <class T,list<int> &t> 
class Tops
{
private:
    list<stack<T>> tops;
public:
    list<stack<T> > getTops() {

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

https://stackoverflow.com/questions/17900247

复制
相关文章

相似问题

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