首页 > 解决方案 > C++ 一个函数上的两个模板关键字

问题描述

观察random.tccSTL 源文件发现以下算子定义:

  template<typename _IntType>
    template<typename _UniformRandomNumberGenerator>
      typename geometric_distribution<_IntType>::result_type
      geometric_distribution<_IntType>::
      operator()(_UniformRandomNumberGenerator& __urng,
         const param_type& __param)
      {
    // About the epsilon thing see this thread:
    // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html
    const double __naf =
      (1 - std::numeric_limits<double>::epsilon()) / 2;
    // The largest _RealType convertible to _IntType.
    const double __thr =
      std::numeric_limits<_IntType>::max() + __naf;
    __detail::_Adaptor<_UniformRandomNumberGenerator, double>
      __aurng(__urng);

    double __cand;
    do
      __cand = std::floor(std::log(1.0 - __aurng()) / __param._M_log_1_p);
    while (__cand >= __thr);

    return result_type(__cand + __naf);
      }

谷歌和各种 C++ 参考资料并没有帮助我理解template连续有两个声明时的含义。请知道的人帮忙。

标签: c++templates

解决方案


它是模板中的模板。

从可能看起来像这样的声明中更容易看到

template<typename _IntType>
class geometric_distribution
{
    ...
    template<typename _UniformRandomNumberGenerator>
    result_type operator()(_UniformRandomNumberGenerator& __urng, const param_type& __param);
    ...
};

推荐阅读