C++ 类模板中非类型参数的模板参数
2015-03-28 11:06
267 查看
16.4.2. Template Arguments for Nontype Parameters
Now that we've seen more about how class templates are implemented, we can look at nontype parameters for class templates. We'll do so by defining a new version of the Screenclass first introduced
in Chapter 12. In this case, we'll redefine Screento be a template, parameterized by its height and width:
template <int hi, int wid>
class Screen {
public:
// template nontype parameters used to initialize data members
Screen(): screen(hi * wid, '#'), cursor (0),
height(hi), width(wid) { }
// ...
private:
std::string screen;
std::string::size_type cursor;
std::string::size_type height, width;
};
This template has two parameters, both of which are nontype parameters. When users define Screenobjects, they must provide a constant expression to use for each of these parameters. The class uses these parameters in the default constructor to set the size
of the default Screen.
As with any class template, the parameter values must be explicitly stated whenever we use the Screentype:
Screen<24,80> hp2621; // screen 24 lines by 80 characters
The object hp2621uses the template instantiation Screen<24, 80>. The template argument for hi is 24, and the argument for widis 80. In both cases, the template argument is a constant expression.
Note: Nontype template arguments must be compile-time constant expressions.
Now that we've seen more about how class templates are implemented, we can look at nontype parameters for class templates. We'll do so by defining a new version of the Screenclass first introduced
in Chapter 12. In this case, we'll redefine Screento be a template, parameterized by its height and width:
template <int hi, int wid>
class Screen {
public:
// template nontype parameters used to initialize data members
Screen(): screen(hi * wid, '#'), cursor (0),
height(hi), width(wid) { }
// ...
private:
std::string screen;
std::string::size_type cursor;
std::string::size_type height, width;
};
This template has two parameters, both of which are nontype parameters. When users define Screenobjects, they must provide a constant expression to use for each of these parameters. The class uses these parameters in the default constructor to set the size
of the default Screen.
As with any class template, the parameter values must be explicitly stated whenever we use the Screentype:
Screen<24,80> hp2621; // screen 24 lines by 80 characters
The object hp2621uses the template instantiation Screen<24, 80>. The template argument for hi is 24, and the argument for widis 80. In both cases, the template argument is a constant expression.
Note: Nontype template arguments must be compile-time constant expressions.
相关文章推荐
- 类模板中非类型参数的模板实参
- 类模板中非类型参数的模板实参
- C++ - 非类型模板参数(nontype template parameters) 使用 及 代码
- C语言简单宏函数实现C++模板功能(类型本身作为函数参数)
- C++模板参数类型(转载)
- c++ template笔记(3)非类型模板参数nontype template parameters
- 类模板,多种类型的类模板,自定义类模板,类模板的默认类型,数组的模板实现,友元和类模板,友元函数,类模板与静态变量,类模板与普通类之间互相继承,类模板作为模板参数,类嵌套,类模板嵌套,类包装器
- C# 调用Dll中非托管C++代码时,函数参数的类型对照
- C++ - 非类型模板参数(nontype template parameters) 使用 及 代码
- [c++][语言语法]函数模板和模板函数 及参数类型的运行时判断
- C# 调用Dll中非托管C++代码时,函数参数的类型对照
- C++模板应用——在类模板实现中引用其内部定义的复合数据类型
- C++:为什么unique_ptr的Deleter是模板类型参数,而shared_ptr的Deleter不是?
- 函数调用,C# 调用Dll中非托管C++代码时,函数参数的类型对照
- C# 调用Dll中非托管C++代码,函数参数的类型对照
- c++ template笔记(3)非类型模板参数nontype template parameters
- C++ 类模板与无类型参数
- C# 调用Dll中非托管C++代码时,函数参数的类型对照
- C++ 获取可变模板参数类型
- c++无类型参数模板(non-type template parameter)