C++ 不带<;的模板方法实现&燃气轮机;

C++ 不带<;的模板方法实现&燃气轮机;,c++,templates,gcc,c++03,C++,Templates,Gcc,C++03,我在课堂上有一个模板方法: template<class T> A& doSomething(T value) 带模板的最后5行的确切含义是什么 谢谢这行代码没有实现。对于类型char const*,char,int,int*,int*和double template A& A::doSomething(char const*); template A& A::doSomething(char); template A& A::doSomet

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

template<class T>
    A& doSomething(T value)
带模板的最后5行的确切含义是什么


谢谢

这行代码没有实现。对于类型
char const*
char
int
int*
int*
double

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);
它是,强制使用指定的模板参数实例化模板,并防止其隐式实例化

显式实例化定义强制对 它们所指的函数或成员函数。它可能出现在 在模板定义之后的任意位置,针对给定的 参数列表,只允许在程序中出现一次

显式实例化声明(外部模板)防止 隐式实例化:否则会导致 隐式实例化必须使用显式实例化 程序中其他地方提供的定义

看看



<>因为您标记了C++ 03,请注意,链接页面中提到的外部模板>代码>是从C++ 11引入的。

没有C++的代码>代码>03@Garf365补充答案。谢谢,现在我明白了。我认为这样做的用例是,当您链接到在cpp文件中实现了模板的库时,避免未定义的引用。我说的对吗?@Jan是的,这确实是显式实例化的一个用例。@Jan是的,从库开发人员的角度来看,您可以限制最终用户只使用最终版本。使用其他链接将导致链接错误。
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);
...
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);