c – 没有<>的模板方法实现

我在课堂上有一个模板方法

template<class T>
    A& doSomething(T value)

然后我有一个实现

template<class T>
    A& A::doSomething(T value){
        // do something with value
        return *this;
    }

然后,我有一个专业,让我们说bool

template<>
    A& A::doSomething(bool value){
        // do something special with value of type bool
        return *this
    }

这是我的理解,现在在代码中有类似的东西我不知道是什么意思:

template A& A::doSomething(char const*);
template A& A::doSomething(char);
template A& A::doSomething(int);
template A& A::doSomething(int*);
template A& A::doSomething(double);
...

具有模板的最后5行的确切含义是什么?

谢谢

解决方法

它是 Explicit instantiation,它强制使用指定的模板参数实例化模板,并阻止它们的隐式实例化.

An explicit instantiation deFinition forces instantiation of the
function or member function they refer to. It may appear in the
program anywhere after the template deFinition,and for a given
argument-list,is only allowed to appear once in the program.

An explicit instantiation declaration (an extern template) prevents
implicit instantiations: the code that would otherwise cause an
implicit instantiation has to use the explicit instantiation
deFinition provided somewhere else in the program.

Explicit instantiation – when is it used?

因为您标记了c 03,请注意链接页面中提到的extern模板是从c 11引入的.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...