I have a class:
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
I know, that it’s not the best idea to move template implementation in cpp file (because it won’t be seen from other cpp’s, which could include the header with the template declaration).
But what about private functions? Could anyone tell me if there are cons of implementing of private template functions in a .cpp file?
When a function template is used in a way that triggers its instantiation, a compiler(at some point) needs to see that template’s definition. And that is the reason, templates are usually implemented inside a header file using inline finctions.
So as long as the above rules gets followed it is still okay to have interface and implementation separated in header and source files.
Reference:
C++03 standard, § 14.7.2.4: