C++ 模版参数

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&) ;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值