C++11&14新标准——模板模板参数(Template Template Parameter)、using

系列文章目录

C++11&14新标准——Variadic templates(数量不定的模板参数)
C++11&14新标准——Uniform Initialization(统一初始化)、Initializer_list(初始化列表)、explicit
C++11&14新标准—— =delete、=default
C++11&14新标准——模板模板参数(Template Template Parameter)、using



1. 模板模板参数(Template Template Parameter)

   模板模板参数(Template Template Parameter)的意思是在原有的模板参数的基础上,在内部再嵌入一个模板参数,实现一些模板容器的功能。
   例如下图的test_moveable函数,内部需要一个模板容器,容器成员类型也为模板,如果直接使用两个模板参数,这样是行不通的,报错信息为:Container不是一个模板。
在这里插入图片描述
   但可以通过模板参数+迭代器+萃取器的方法,实现这个功能。输入一个模板容器,通过对这个模板容器的迭代器进行萃取,得到迭代器所指类型的型别,实现这个功能,这是一种可行的方法。
在这里插入图片描述
   但更好的方式是使用模板模板参数。可以看到container既是一个模板参数,同时他自身也是一个模板,这样就可以实现模板容器。
在这里插入图片描述

2. Using

上文设计好XCIs类之后,尝试用vector去实例化一个对象,但会发现报错。
在这里插入图片描述
这其实是因为我们的XCIs类第二个模板参数container,他自身作为一个模板只能接受一个参数,但vector其实有两个参数——数据类型和分配器,第二个参数有默认值。
在这里插入图片描述
这时候就可以通过using关键词,将之前的两参数容器提前处理,变为单一参数模板,从而可以正常实例化。
在这里插入图片描述
在这里插入图片描述

Using、#define、typedef

#define是一个预处理指令,用于进行文本替换,不仅仅用于定义类型别名,还可用于宏定义、条件编译等。

#define MyInt int
#define DoubleVector std::vector<double>

typedef是传统的 C++ 中用于定义类型别名的关键字,它在 C++98 就已存在,typedef 用于给类型起别名。

typedef int MyInt;
typedef std::vector<double> DoubleVector;

using 是 C++11 引入的关键字,用于定义类型别名。它提供了更直观、类型安全的方式来定义别名。格式为:

using MyInt = int;
using DoubleVector = std::vector<double>;

using最大的特点就是支持模板操作,可以用于模板别名,支持模板参数的自动推导。这也是他与typedef最大的区别。

  • 29
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ templates are a powerful feature of the C++ programming language that allow generic programming. Templates enable the creation of functions and classes that can work with different data types without the need for separate implementations for each data type. Templates are defined using the keyword "template" followed by a list of template parameters enclosed in angle brackets "< >". The template parameters can be either type parameters or non-type parameters, depending on whether they represent a data type or a value. For example, a type parameter might be used to specify the data type of a container class, while a non-type parameter might be used to specify the size of an array. Here is an example of a simple function template that returns the maximum of two values: ```c++ template<typename T> T max(T a, T b) { return a > b ? a : b; } ``` In this example, the "typename" keyword is used to indicate that T is a type parameter. The function can be used with any data type for which the ">" operator is defined. Templates can also be used to define class templates, which are similar to regular classes but can work with different data types. Here is an example of a simple class template for a stack: ```c++ template<typename T> class Stack { public: void push(T value); T pop(); private: std::vector<T> data_; }; template<typename T> void Stack<T>::push(T value) { data_.push_back(value); } template<typename T> T Stack<T>::pop() { T value = data_.back(); data_.pop_back(); return value; } ``` In this example, the class template is defined with a single type parameter T. The member functions push and pop are defined outside the class definition using the scope resolution operator "::". Templates are a powerful tool that can greatly simplify code and make it more reusable. However, they can also be complex and difficult to debug. It is important to use templates judiciously and to thoroughly test them with a variety of data types.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值