C++类模板参数
创始人
2024-02-07 08:55:16
0

C++类模板参数

12.4 C++ non-type

12.4.1 格式

template 
//这里的n就是non-type(表达式或参数)

12.4.2 non-type的几点要求

  • 1.可以是int,枚举,参考,指针类型;不允许double类型,但是允许是double的参考和指针类型
  • 2.不能更改n的值,也不可以获取n的地址
  • 3.当初始化n时,要求是常量(constant)

12.4.3 non_type的优缺点

优点:存储在存储自动变量的堆栈中,相比于使用new and delete自动内存分配来说速度快一些,特别对于小数组。
缺点:每个数组大小都生成自己的模板,也就是根据模板和大小(只要两者任意一个与之前的有不同)生成各自的类声明,会占用存储空间。
但是不包含non-type的模板类是使用类构造函数确定大小的,因此只需要一个声明。
(类构造函数更通用,适合将某个大小的数组赋值给另一个大小的数组或者调整数组大小)

12.4.4 举例—arraytp类

arraytp.h

#pragma once
//arraytp.h -- Array Template
#ifndef ARRAYTP_H_
#define ARRAYTP_H_
#include 
#include 
#include
template 
class ArrayTP
{
private:T ar[n];
public:ArrayTP() {};explicit ArrayTP(const T& v);virtual T& operator[](int i);virtual T operator[](int i) const;
};
template 
ArrayTP::ArrayTP(const T& v)
{for (int i = 0; i < n; i++)ar[i] = v;
}
template 
T& ArrayTP::operator[](int i)
{if (i < 0 || i >= n){std::cerr << "Error in array limits: " << i<< " is out of range\n";std::exit(EXIT_FAILURE);}return ar[i];
}
template 
T ArrayTP::operator[](int i) const
{if (i < 0 || i >= n){std::cerr << "Error in array limits: " << i<< " is out of range\n";std::exit(EXIT_FAILURE);}return ar[i];
}
template
using arrtype = ArrayTP; // template to create multiple aliases
#endif

12.6 C++对模板类pairs

12.6.1 简介

模板包含多个类型参数。

格式:

template 

12.6.2 代码

pairs.h

#pragma once
#ifndef PAIRS_H_
#define PAIRS_H_
//双模板的声明
template 
class Pair
{
private:T1 a;T2 b;
public:T1& first();T2& second();T1 first() const { return a; }T2 second() const { return b; }Pair(const T1& aval, const T2& bval) : a(aval), b(bval) { }Pair() {}
};
template
T1& Pair::first()
{return a;
}
template
T2& Pair::second()
{return b;
}
#endif

main.h

#pragma once
#ifndef MAIN_H_
#define MAIN_H_
#include  //输入输出 
#include  //simple_template、template_pairs
#include "pairs.h" //template_pairs
using namespace std;
void template_pairs(void)
{std::cout << "\ntemplate_pairs Hello******************************************************\n";using std::cout;using std::endl;using std::string;Pair ratings[4] ={Pair("The Purpled Duck", 5),//Pair这个是为了触发Pair类的构造函数Pair("Jaquie's Frisco Al Fresco", 4),Pair("Cafe Souffle", 5),Pair("Bertie's Eats", 3)};int joints = sizeof(ratings) / sizeof(Pair);cout << "Rating:\t Eatery\n";for (int i = 0; i < joints; i++)cout << ratings[i].second() << ":\t "<< ratings[i].first() << endl;cout << "Oops! Revised rating:\n";ratings[3].first() = "Bertie's Fab Eats";ratings[3].second() = 6;cout << ratings[3].second() << ":\t "<< ratings[3].first() << endl;std::cout << "template_pairs Bye**************************************************************\n";
}
#endif

main.cpp

/*
Project name :			_12template
Last modified Date:		2022年5月6日11点33分
Last Version:			V1.0
Descriptions:			对模板类pairs
*/
#include "main.h"int main()
{cout << "对模板类pairs******************************************************************" << endl;template_pairs();return 0;
}

12.6.3 运行结果

对模板类pairs******************************************************************
template_pairs Hello******************************************************
Rating:  Eatery
5:       The Purpled Duck
4:       Jaquie's Frisco Al Fresco
5:       Cafe Souffle
3:       Bertie's Eats
Oops! Revised rating:
6:       Bertie's Fab Eats
template_pairs Bye**************************************************************D:\Prj\_C++Self\_12template\Debug\_12template.exe (进程 12356)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .

12.7 C++默认类型模板参数

12.7.1 格式

template  class Topo{...};

12.7.2 意义

就是默认情况下T2的类型为int(和默认参数差不多,给值就为给的值,不给值就为默认值)

12.7.3 注意事项

可以对类模板使用默认模板参数,不能对函数模板使用默认模板参数;但是可以为类模板或函数模板的non-type提供默认参数。

12.10 模板作为模板的参数

12.10.1 定义

一个模板也可以作为别的模板的参数(例子是Crab类)

12.10.2 格式

template