博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ 定义函数模板
阅读量:4088 次
发布时间:2019-05-25

本文共 2990 字,大约阅读时间需要 9 分钟。

16.1.1. Defining a Function Template

The following is a template version of compare:

// implement strcmp-like generic compare function
// returns 0 if the values are equal, 1 if v1 is larger , -1 if v1 is smaller
template <typename T>
int compare(const T &v1, const T &v2)
{
if (v1 < v2) return -1;
if (v2 < v1) return 1;
return 0;
}
A  template definition starts with the keyword templatefollowed by a template parameter list  , which is a comma-separated list of one or more template parameters  bracketed by the lessthan (<) and greater-than (>) tokens.

Note:The template parameter list cannot be empty.

Template Parameter List

Analogously, template parameters represent types or values we can use in the definition of a class or function. For example, our comparefunction declares one type parameter named T. Inside  compare, we can use the name T to refer to a type. Which actual type Trepresents is determined by the compiler based on how the function is used.

A  template parameter can be a type parameter  ,  which represents a type, or a nontype parameter  ,  which represents a constant expression. A nontype parameter is declared following a  type specifier. We'll see more about nontype parameters in Section 16.1.5. A type parameter is defined following the keyword class or typename. For example, class T is a type parameter named T. There is no difference between classand typenamein this context.

Using a Function Template

When we use a function template, the compiler infers what template argument(s)  to bind to the template parameter(s). Once the compiler determines the actual template argument(s), it instantiates an instance of the function template for us. Essentially, the compiler figures out what type to use in place of each type parameter and what value to use in place of each nontype parameter. Having deduced the actual template arguments, it generates and compiles a  version of the function using those arguments in place of the corresponding template parameters. The compiler takes on the tedium of (re)writing the function for each type we use.

int main ()

{
// T is int;
// compiler instantiates int compare(const int&, const int&)
cout << compare(1, 0) << endl;
// T is string;
// compiler instantiates int compare(const string&, const string&)
string s1 = "hi", s2 = "world";
cout << compare(s1, s2) << endl;
return 0;
}

the compiler will instantiate two different versions of  compare. The compiler will create one version that replaces Tby intand a second version that uses stringin place of  T.

inlineFunction Templates

A  function template can be declared inlinein the same way as a nontemplate function. The specifier is placed following the template parameter list and before the return type. It is not placed in front of the template keyword.

// ok: inline specifier follows template parameter list
template <typename T> inline T min(const T&, const T&);
// error: incorrect placement of inline specifier
inline template <typename T> T min(const T&, const T&);

转载地址:http://ryyii.baihongyu.com/

你可能感兴趣的文章
DirectX11 HLSL打包(packing)格式和“pad”变量的必要性
查看>>
DirectX11 光照演示示例Demo
查看>>
漫谈一下前端的可视化技术
查看>>
VUe+webpack构建单页router应用(一)
查看>>
Vue+webpack构建单页router应用(二)
查看>>
从头开始讲Node.js——异步与事件驱动
查看>>
Node.js-模块和包
查看>>
Node.js核心模块
查看>>
express的应用
查看>>
NodeJS开发指南——mongoDB、Session
查看>>
Express: Can’t set headers after they are sent.
查看>>
2017年,这一次我们不聊技术
查看>>
实现接口创建线程
查看>>
Java对象序列化与反序列化(1)
查看>>
HTML5的表单验证实例
查看>>
JavaScript入门笔记:全选功能的实现
查看>>
程序设计方法概述:从面相对象到面向功能到面向对象
查看>>
数据库事务
查看>>
JavaScript基础1:JavaScript 错误 - Throw、Try 和 Catch
查看>>
SQL基础总结——20150730
查看>>