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

c – 私人模板功能

发布时间:2020-12-16 06:01:08 所属栏目:百科 来源:网络整理
导读:我有一个班: C.h class C {private: templateint i void Func(); // a lot of other functions}; C.cpp // a lot of other functionstemplateint ivoid C::Func() { // the implementation}// a lot of other functions 我知道,在cpp文件中移动模板实现并不
我有一个班:

C.h

class C {
private:
  template<int i>
  void Func();

  // a lot of other functions
};

C.cpp

// a lot of other functions

template<int i>
void C::Func() {
 // the implementation
}

// a lot of other functions

我知道,在cpp文件中移动模板实现并不是最好的办法(因为它不会从其他cpp看到,可能包括带有模板声明的头文件).

但私人功能呢?有没有人可以告诉我,在.cpp文件中是否存在实现私有模板功能的缺点?

解决方法

当以触发其实例化的方式使用函数模板时,编译器(在某些时候)需要看到该模板的定义.这就是原因,模板通常使用内联功能在头文件中实现.

所以只要遵守上述规则,仍然可以在头文件和源文件中分离接口和实现.

参考:
C 03标准,§14.7.2.4:

The definition of a non-exported function template,a non-exported member function template,or a non-exported member function or static data member of a class template shall be present in every translation unit in which it is explicitly instantiated.

(编辑:李大同)

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

    推荐文章
      热点阅读