2008 December 24th Wednesday

   After C++ experimented with the approaches described later, the C++ Standard settled upon this approach:

  For a given function name, you can have a non-template function, a template function, and an explicit specialization
template function.

  The prototype and definition for an explicit specialization should be preceded by template <> and should mention
the specialized type by name.

  A specialization overrides the regular template, and a non-template function overrides both.

  Here's how prototypes for swapping type job structures would look for these three forms:

// non-template function prototype
void Swap(job &, job &);

// template prototype
template <class Any>
void Swap(Any &, Any &);

// explicit specialization for the job type
template <> void Swap<job>(job &, job &);

  As mentioned previously, if more than one of these prototypes is present, the compiler chooses the non-template
version over explicit specializations and template versions, and it chooses an explicit specialization over a version
generated from a template.

  When the compiler uses the template to generate a function definition for a particular type, the result is termed
an instantiation of the template.

  Originally, implicit instantiation was the only way the compiler generated function definitions from templates,
but now C++ allows for explicit instantiation. That means you can instruct the compiler to create a particular
instantiation, for example, Swap<int>(), directly. The syntax is to declare the particular variety you want, using
the <> notation to indicate the type and prefixing the declaration with the keyword template:

template void Swap<int>(int, int);  // explicit instantiation

  A compiler that implements this feature will, upon seeing this declaration, use the Swap() template to generate
an instantiation using the int type. That is, this declaration means "Use the Swap() template to generate a function
definition for the int type."

  Contrast the explicit instantiation with the explicit specialization, which uses one or the other of these equivalent
declarations:

template <> void Swap<int>(int &, int &);  // explicit specialization
template <> void Swap(int &, int &);       // explicit specialization

  The difference is that these declarations mean "Don't use the Swap() template to generate a function definition.
Instead, use a separate, specialized function definition explicitly defined for the int type." These prototypes have
to be coupled with their own function definitions.

  It is an error to try to use both an explicit instantiation and an explicit specialization for the same type(s) in
the same programming unit.

  Implicit instantiations, explicit instantiations, and explicit specializations collectively are termed specializations.
What they all have in common is that they represent a function definition using specific types rather than one that
is a generic description.

  The addition of the explicit instantiation led to the new syntax of using template and template <> prefixes in
declarations to distinguish between the explicit instantiation and the explicit specialization.

  Instantiations is just a process to form a function Specializations from a template.  Specializations is a result from
an instantiation or from Specializations definitions.

...
template <class Any>
void Swap (Any &, Any &);  // template prototype

template <> void Swap<int>(job &, job &);  // explicit specialization for job
int main(void)
{
  template void Swap<char>(char &, char &);  // explicit instantiation for char
  short a, b;
  ...
  Swap(a,b);   // implicit template instantiation for short
  job n, m;
  ...
  Swap(n, m); // use explicit specialization for job
  char g, h;
  ...
   Swap(g, h);  // use explicit template instantiation for char
   ...
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值