C++模板别名

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。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jasmine-Lily

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值