c – 私人模板功能

我有一个班:

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.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...