I'm implementing a custom templated container as part of a learning project in C++. The container makes use of different components like serialization, memory management, iterators. I am wondering what is the best way to organise the code.
Option 1: Put everything into one header file:
I've seen this in several example online. But by doing this I'm going to end up with a file of several thousand lines long:
// container.hpp
template <typename params>
class Container {
public:
// params is a struct with typedefs and static variables
// used to configure the container at compile time
// I define additional typedefs and static variables here from params::
public:
class iterator {
// can use the typedefs and variables above
};
class memory {
// can use the typedefs and variables above
};
};
Option 2: Split the code into multiple headers
Each file contains the implementation of 1 component.
// memory.hpp
template <typename Container>
class memory {
public:
// typedefs and static variables from Container::
};
// iterator.hpp
template <typename Container>
class iterator {
public:
// typedefs and static variables from Container::
};
// container.hpp
#include "iterator.hpp"
#include "memory.hpp"
template <typename params>
class Container {
public:
// typedefs and static variables here from params::
typedef Container<params> self_type;
iterator<self_type> begin();
private:
memory<self_type> mem;
};
What are the arguments to chose?
Solution 1 seems cleaner; something bugs me about having to pass the container type to the memory and iterators in solution 2. But at the same time the code is organised in different files in solution 2. Each file contains the implementation of 1 component.
Is it just a matter of personal preference? Or are there objective reasons to choose one solution over the other? Or is there a completely different and better approach?