没有<>的模板方法实现

Jan*_*Jan 3 c++ gcc templates c++03

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

template<class T>
    A& doSomething(T value)
Run Code Online (Sandbox Code Playgroud)

然后我有一个实现

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

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

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

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

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);
...
Run Code Online (Sandbox Code Playgroud)

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

谢谢

son*_*yao 5

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

显式实例化定义强制实例化它们引用的函数或成员函数.它可能在模板定义之后的任何地方出现在程序中,并且对于给定的参数列表,只允许在程序中出现一次.

显式实例化声明(extern模板)可防止隐式实例化:否则会导致隐式实例化的代码必须使用程序中其他位置提供的显式实例化定义.

并看到明确的实例化 - 何时使用?


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