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]
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]
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 ***/