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

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

As with a function parameter, the name chosen by the programmer for a template parameter has no intrinsic meaning. In our example, we named compare's template type parameter T, but we could have named it anything:

// equivalent template definition
template <class Glorp>
int compare(const Glorp &v1, const Glorp &v2)
{
if (v1 < v2) return -1;
if (v2 < v1) return 1;
return 0;

}

This code defines the same comparetemplate as before.

Template Parameter Scope

The name of a template parameter can be used after it has been declared as a template

parameter and until the end of the template declaration or definition.
Template parameters follow normal name-hiding rules. A template parameter with the same
name as an object, function, or type declared in global scope hides the global name:

typedef double T;
template <class T> T calc(const T &a, const T &b)
{
// tmp has the type of the template parameter T
// not that of the global typedef
T tmp = a;
// ...
return tmp;
}

Restrictions on the Use of a Template Parameter Name

A  name used as a template parameter may not be reused within the template:

template <class T> T calc(const T &a, const T &b)
{
typedef double T; // error: redeclares template parameter T
T tmp = a;
// ...
return tmp;
}

This restriction also means that the name of a template parameter can be used only once within

the same template parameter list:
// error: illegal reuse of template parameter name V
template <class V, class V> V calc(const V&, const V&) ;

Of course, just as we can reuse function parameter names, the name of a template parameter

can be reused across different templates:

// ok: reuses parameter type name across different templates

template <class T> T calc (const T&, const T&) ;
template <class T> int compare(const T&, const T&) ;

Template Declarations

As with any other function or class, we can declare a template without defining it. A declaration

must indicate that the function or class is a template:

// declares compare but does not define it
template <class T> int compare(const T&, const T&) ;

The names of the template parameters need not be the same across declarations and the

definition of the same template:

// all three uses of calc refer to the same function template
// forward declarations of the template
template <class T> T calc(const T&, const T&) ;
template <class U> U calc(const U&, const U&) ;
// actual definition of the template
template <class Type>
Type calc(const Type& a, const Type& b) { /* ... */ }

Each template type parameter must be preceded either by the keyword classor typename;

each nontype parameter must be preceded by a type name. It is an error to omit the keyword
or a type specifier:

// error: must precede U by either typename or class
template <typename T, U> T calc (const T&, const U&) ;

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

你可能感兴趣的文章
ABAP项目砖家之旅-ABAP对象命名规则
查看>>
SAP接口集成-PO/PI-SLD配置
查看>>
SAP接口集成-abap调用外部数据库
查看>>
abap实现大数据-echar调用
查看>>
SAP财务凭证校验和替换
查看>>
java编程之伪静态(urlrewrite)
查看>>
SpringMVC+Mybatis 多数据源配置
查看>>
springboot/cloud使用redis存储对象
查看>>
JVM之常用启动参数(扩展参数)
查看>>
同步/异步 阻塞/非阻塞
查看>>
Java中高级进阶之路:Java基础篇——HashMap(ConcurrentHashMap)
查看>>
linx项目部署常用指令
查看>>
微信小程序模板消息推送实现(java后台)(微信平台已下架该接口)
查看>>
微信小程序支付接口实现(java后台)
查看>>
JDBC原生写法+原生连接池
查看>>
Linux搭建LNMP并部署禅道项目管理
查看>>
Linux下配置java环境
查看>>
Linux7安装图形界面
查看>>
linux(服务器)CentOS下配置mysql
查看>>
两种方式部署定时任务Spring整合Quartz、Spring整合Task(java后台)
查看>>