24

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?

Alek86
  • 1,489
  • 3
  • 17
  • 26
  • http://stackoverflow.com/questions/4315969/where-to-define-c-class-member-template-function-and-functors-that-instantiate/4316020#4316020 – Johannes Schaub - litb Sep 17 '11 at 14:21
  • 1
    While the question was technically answered by Als and Nicola, for me this just begs the question why you made this a member template at all. Wouldn't it be much better to have this as a free function template in (the unnamed namespace of) the cpp file? I have found that, over time, I used private member functions less and less, more and more resorting to free functions in (the unnamed namespace of) the class' implementation file. – sbi Nov 08 '11 at 19:53

2 Answers2

19

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:

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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Is this not contradictory : `So as long as the above rules gets followed it is still okay to have interface and implementation separated in header and source files` .. **contradicts** >> `And that is the reason, templates are usually implemented inside a header file using inline finctions.`, or did I miss something? – Nawaz Sep 14 '11 at 15:27
  • @Nawaz: Updated to answer your Question. Hth. – Alok Save Sep 14 '11 at 15:35
  • That doesn't answer my question. You said, the definition can be in .cpp file, how? – Nawaz Sep 14 '11 at 15:38
  • @Nawaz: As standard Quotes "The defintion must be present in the same Translation Unit(headers + source file)". If the function is private, the template function will most likely ***Only** be explicitly instantiated from the same source file which has the defintion of the template function, that satisfy's the requirement stated by the Standard. – Alok Save Sep 14 '11 at 17:56
13

Unless your private member function template is used by member functions that are defined inline within the class definition, I see nothing wrong with this approach. On the contrary, I think that the less dependencies creep into your header files, the better.

This will work as long as you enforce the convention of always providing each class's implementation in a single source file.

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55