Is it a good practice to not to separate function declarations and definitions for templated classes ?

Hi Everyone :),

Generally in non templated classes, we separate the function declarations and definitions into separate files (.h and .cpp)

[1] But the above practice does not seem to work very good with templated classes. Is it advised to write implementations in a separate file and then include it at the bottom of .h file ?

[2] Which of the below scheme is generally advised for templated classes?
[a] declarations and definition all at once or
[b]seperated declarations & definitions in the same file

Given the complicated syntax that we have to take care of if we go by choice [b]


Eg.

[a]
1
2
3
4
5
6
7
8
9
10
11
12
13
  template <typename T>
  class unique_ptr final {
    private:
      T* ptr_;
    public:
      unique_ptr (T* ptr = nullptr) noexcept {
        : ptr_{ ptr } {
      }
      
      friend bool operator == (const unique_ptr& lhs, const unique_ptr& rhs) {
        return lhs.get() == rhs.get();
      }
  };


[b]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  template <typename T>
  class unique_ptr final {
    private:
      T* ptr_;
    public:
      unique_ptr (T* ptr = nullptr) noexcept;      
      friend bool operator == (const unique_ptr& lhs, const unique_ptr& rhs);

/*** implementations inside the class after all declarations (I am not sure if this makes the code code any easier to understand)  ***/
  };

/**** Implementations outside the class ***/
/***  Convoluted things needed to make friend functions work ***/
/** like mentioned in : https://stackoverflow.com/questions/3989678/c-template-friend-operator-overloading  ***/
Last edited on
templates, I put everything in the .h file usually. If the function bodies are huge, you can find ways to break it up, but most templates (in my experience) tend to be smallish.
kapil2905 wrote:
Generally in non templated classes, we separate the function declarations and definitions into separate files

It may not be as general as you might think: there are pros and cons both ways (although the most benefit from defining functions in headers is realized when you define everything in the headers, resulting in a header-only library)

kapil2905 wrote:
Is it advised to write implementations in a separate file and then include it at the bottom of .h file
kapil2905 wrote:
declarations and definition all at once
kapil2905 wrote:
seperated declarations & definitions in the same file


All three approaches are widely used. I don't think there's a clear winner. The only potential downside I know of that isn't just personal taste is that #1 makes it slightly harder to transition to Modules (when they arrive) because those separate files containing definitions do not compile on their own.

That said, your example deals with operator template friends, which are an unusual case. The two approaches shown in the StackOverflow link do different things (one friends a non-template operator+, the other friends a specialization of a template). I prefer the first approach (defining a non-template friend in the class body), for the same reason as for operator overloading in non-template classes: it simplifies compilation and leads to cleaner errors when this operator is used incorrectly.
Last edited on
Thanks Jonnin and Cubbi for your explanations :)
If a method is more than 2-3 lines I define it separately from the class declaration. This is true whether or not the class is a template.

The reason is to preserve the sanity of the poor programmer who's using my class. When they look at the header file, they want to see what the methods are and how to use them. They don't want to have to skip over 100 lines of code just to see the next method in the class.

So keep the method declarations and definitions separate. For a template the definitions must be present so it doesn't matter much if they are in the same file or a separate file that's #included at the end.
Topic archived. No new replies allowed.