指向具有外部链接到非类型模板参数的对象的指针 [英] pointers to object with external linkage to Nontype template parameters

查看:391
本文介绍了指向具有外部链接到非类型模板参数的对象的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过下面的代码。

  template< int VAL> 
void printVAL()
{
for(int i = 0; i cout< i value is<< i<< endl;
}
}



实例化:
printVAL< 100> )



当我使用( std :: string s )作为非类型模板参数时,编译器向我喊话, / p>

 std :: basic_str< char>'不是模板非类型参数的有效类型

我知道我们应该只使用常数积分值,甚至不要使用双精度。



问题:



1)为什么我们不应该使用std :: string,

2)可以使用外部链接的对象的指针的含义。可以获取任何示例代码吗?

解决方案

根据14.1 / 4,


非类型模板参数具有以下(可选cv限定)类型之一:




  • 积分或枚举类型,

  • 指向对象或指向函数的指针

  • 引用对象或引用函数的左值引用,

  • 指向成员的指针,

  • std :: nullptr_t


您的 std :: string 不是这些。


I tried the following code.

template <int VAL>
void printVAL()
{
    for(int i=0;i<VAL; i++){
        cout << " i value is "<<i<<endl;
    }
}

instantiation: printVAL<100>()

When i use (std::string s ) as a non type template parameter, the compiler shouted at me with the following error

"class std::basic_str<char>' is not a valid type for a template non-type parameter.

What i know is we should use only constant integral values only. Not even double.

Question:

1) why we should not use std::string, what bothers ?

2) What is the meaning of 'pointers to objects with external linkage can be used'. can i get any sample code for it?

解决方案

According to 14.1/4,

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,
  • lvalue reference to object or lvalue reference to function,
  • pointer to member,
  • std::nullptr_t.

Your std::string is none of those.

这篇关于指向具有外部链接到非类型模板参数的对象的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆