模板化类的方法是否暗含内联? [英] Are methods of templated classes implied inline linkage?

查看:69
本文介绍了模板化类的方法是否暗含内联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模板化类的方法是否暗含inline链接(不是在讨论内联优化),还是仅仅是模板化方法?

Are methods of templated classes implied inline linkage (not talking about the inline optimization), or is it just templated methods which are?

// A.h

template<typename T>
class A
{
public:
    void func1();                       //  #1
    virtual void func2();               //  #2
    template<typename T2> void func3(); //  #3
};

template<typename T>
void A<T>::func1(){}    //  #1

template<typename T>
void A<T>::func2(){}    //  #2

template<typename T>
template<typename T2>
void A<T>::func3<T2>(){}    //  #3

是否所有上述情况inline [链接]? (我是否应该为其中的任何一个明确地写inline)?

Are all the above cases inline [linkage]? (Should I explicitly write inline for any of them)?

推荐答案

如果模板函数和模板类的成员函数是隐式实例化的,则它们是隐式内联的,但是请注意,模板专业化不是.

Template functions and member functions of template classes are implicitly inline if they are implicitly instantiated, but beware template specializations are not.

template <typename T>
struct test {
    void f();
}
template <typename T>
void test<T>::f() {}           // inline

template <>
void test<int>::f() {}           // not inline


由于缺少更好的报价:


By lack of a better quote:

除非在某个翻译单元中显式实例化了相应的专业化(14.7.2),否则必须在隐式实例化该模板的每个翻译单元中定义一个非导出模板(14.7.1);无需诊断

A non-exported template must be defined in every translation unit in which it is implicitly instantiated (14.7.1), unless the corresponding specialization is explicitly instantiated (14.7.2) in some translation unit; no diagnostic is required

这篇关于模板化类的方法是否暗含内联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆