且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

模板化类的方法是否暗含内联?

更新时间:2023-11-27 18:45:34

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

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