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

标签 c++ templates gcc c++03

我在类中有一个模板方法:

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 template 是从 c++11 引入的。

关于c++ - 没有<>的模板方法实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35766734/

相关文章:

c++ - cpp中的cin对象

c++ - 函数指针 vs 函数作为模板非类型参数

c++ - 从先前的模板参数获取类型

c++ - 收到基于模板类及其子类函数调用的错误

html - Symfony2 - 渲染模板时获取外部内容

c - 数学函数不适用于 C 中的 "make"

Linux下Visual Studio与gcc的C++兼容性

c++ - 如何将同一行中的值分组为一对?

c++ - 在模板参数上使用隐式命名空间

c - 为 ARM 编译时出现 GCC 错误