加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 10:01:56 所属栏目:百科 来源:网络整理
导读:我在课堂上有一个模板方法: templateclass T A doSomething(T value) 然后我有一个实现 templateclass T A A::doSomething(T value){ // do something with value return *this; } 然后,我有一个专业,让我们说bool template A A::doSomething(bool value){
我在课堂上有一个模板方法:

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引入的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读