模板到底接收什么类型的非类型参数???

NICK6918 2014-08-28 03:06:53
模板到底接收什么类型的参数??

对这样一个模板函数:
template <int a,int b>
int add (){
return a+b;
};


这样调用是对的 add<3,4>();
但是,换成变量,无论是const与否的对象,对象引用,对象右值引用都不可以,显示找不到匹配的方程
const int& n=3;
add<n,4>();


当n为int,const int, int&, const int&, int&&, const int&&都不可以!!!

为什么
...全文
254 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
winnuke 2014-08-29
  • 打赏
  • 举报
回复
模板参数都是在编译期间就确定下来的,你用一个变量来做参数怎么能编译通过呢?
jwj070524 2014-08-29
  • 打赏
  • 举报
回复
不一定是全局的,如果模板参数是值类型的话,编译期常量就行了啊。 模板参数(不管是类型还是值)必须是在编译期间能够确定下来的:(1)如果是引用,那么变量必须是全局的,有些时候必须加extern编译器才给过(2)如果是指针,不管是变量的指针还是函数的指针,也必须是全局的(3)如果是值,那么值必须在编译期间能够确定,因此可以是某个作用域中的常量 之所以要全局,那是因为模板实例化在编译期间就完成了,而局部变量存储在栈上,它的地址无法再编译期间确定。
mujiok2003 2014-08-28
  • 打赏
  • 举报
回复
模板在编译时实例化, 而已变量可能要在运行时才确定值。
taodm 2014-08-28
  • 打赏
  • 举报
回复
因为规定! 自己去找本《C++ templates》认真啃,上面都有。 另外,过于深入学习模块,或者过于刨根问底在这些细节上,约等于浪费生命。
NICK6918 2014-08-28
  • 打赏
  • 举报
回复
引用 2 楼 jwj070524 的回复:

8.2.2 Nontype Parameters
Nontype template parameters stand for constant values that can be determined at compile or link time.  The type of such a parameter (in other words, the type of the value for which it stands) must be one of the following:

Template template parameters do not denote types either; however, they are not considered when talking about nontype parameters.

An integer type or an enumeration type

A pointer type (including regular object pointer types, function pointer types, and pointer-to-member types)

A reference type (both references to objects and references to functions are acceptable)
引用类型是可以的呢,上面说了,对象的引用和函数引用都可以啊。

template <int &N>
struct _Ref
{
  int getValue() {return N;}
};

template <int N>
struct _Val
{
  int getValue() {return N;}
};

int global_a; // 全局变量默认为0
int const global_b = 1;

int main ()
{
  _Ref<global_a> instance1; // OK 引用类型必须是全局的
  _Val<global_b> instance2; // OK 值类型必须是编译期常量

  std::cout << instance1.getValue() << std::endl; 
  std::cout << instance2.getValue() << std::endl;
}
确实是这样,确实只有全局的变量(包括指针和引用)才可以在< >内使用,如果是值的话还必须是常量。 我不理解为什么只能是全局变量啊,求解释。。。
jwj070524 2014-08-28
  • 打赏
  • 举报
回复

8.2.2 Nontype Parameters
Nontype template parameters stand for constant values that can be determined at compile or link time.  The type of such a parameter (in other words, the type of the value for which it stands) must be one of the following:

Template template parameters do not denote types either; however, they are not considered when talking about nontype parameters.

An integer type or an enumeration type

A pointer type (including regular object pointer types, function pointer types, and pointer-to-member types)

A reference type (both references to objects and references to functions are acceptable)
引用类型是可以的呢,上面说了,对象的引用和函数引用都可以啊。

template <int &N>
struct _Ref
{
  int getValue() {return N;}
};

template <int N>
struct _Val
{
  int getValue() {return N;}
};

int global_a; // 全局变量默认为0
int const global_b = 1;

int main ()
{
  _Ref<global_a> instance1; // OK 引用类型必须是全局的
  _Val<global_b> instance2; // OK 值类型必须是编译期常量

  std::cout << instance1.getValue() << std::endl; 
  std::cout << instance2.getValue() << std::endl;
}
阿呆_ 2014-08-28
  • 打赏
  • 举报
回复
应该只接收常量。 任何变量都不行(哪怕是const 变量)。

3,882

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧