12.12 C++模板别名
12.12.1 定义
Template Aliases (C++11):
-
1.typedef 数据类型
-
2.template<typename T>
using arrtype = ArrayTP<T, 12>; // template to create multiple aliases
-
3.using = 数据类型---特点是比typedef更清晰嘿嘿
12.12.2 代码
arraytp.h
main.h
#pragma once #ifndef MAIN_H_ #define MAIN_H_ #include <iostream> //输入输出 #include <ctime> //pointer_template、template_aliases #include "arraytp.h" //template_recursion #include<array> //template_aliases using namespace std; void template_aliases() { std::cout << "\ntemplate_aliases Hello************************************************\n"; // define three typedef aliases typedef std::array<double, 12> arrd; typedef std::array<int, 12> arri; typedef std::array<std::string, 12> arrst; arrd gallons; // gallons is type std::array<double, 12> arri days; // days is type std::array<int, 12> arrst months; // months is type std::array<std::string, 12> //有句相关的类型别名在arraytp.h文件中 arrtype<double> gallons1; // gallons is type std::array<double, 12> arrtype<int> days1; // days is type std::array<int, 12> arrtype<std::string> months1; // months is type std::array<std::string, 12> typedef const char* pc1; // typedef syntax using pc2 = const char*; // using = syntax typedef const int* (*pa1)[10]; // typedef syntax using pa2 = const int* (*)[10]; // using = syntax std::cout << "\ntemplate_aliases Bye************************************************\n"; } #endif
main.cpp
/* Project name : _12template Last modified Date: 2022年5月6日11点33分 Last Version: V1.0 Descriptions: 模板总结 */ #include "main.h" int main() { cout << "模板别名******************************************************************" << endl; template_aliases(); return 0; }
12.12.3 运行结果
模板别名****************************************************************** template_aliases Hello************************************************ template_aliases Bye************************************************ D:\Prj\_C++Self\_12template\Debug\_12template.exe (进程 12396)已退出,代码为 0。 要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。 按任意键关闭此窗口. . .