读书人

几行模板代码,算是quot;重载quot;还是算quot;特化quot;

发布时间: 2012-06-08 12:55:24 作者: rapoo

几行模板代码,算是"重载"还是算"特化"呢? 分不清了
VC2010支持的C++11的<functional>里面有下面的代码:

C/C++ code
template<int _Nx>    class _Ph    {    // placeholder    };template<class _Tx>    struct is_placeholder    {    // template to indicate that _Tx is not a placeholder    static const int value = 0;    };template<int _Nx>    struct is_placeholder<_Ph<_Nx> >    {    // template specialization to indicate that _Ph<_Nx> is a placeholder    static const int value = _Nx;    };


这里的两个is_placeholder,算是2个重载呢? 还是下面的<int _Nx>版本算是一个特化呢?
模板函数,怎么才算重载,怎么才算特化?
谢谢!

[解决办法]
主模板
template<class _Tx>
struct is_placeholder
{ // template to indicate that _Tx is not a placeholder
static const int value = 0;
};
特化
template<int _Nx> struct is_placeholder<_Ph<_Nx> > { // template specialization to indicate that _Ph<_Nx> is a placeholder static const int value = _Nx; };
[解决办法]
探讨

引用:
主模板
template<class _Tx>
struct is_placeholder
{ // template to indicate that _Tx is not a placeholder
static const int value = 0;
};
特化
template<int _Nx> struct is_placeholder<_P……

读书人网 >C++

热点推荐