Here's a question: what container should we use to return the sequence from function if we know that the sequence never has more than N elements and N is not big. For example, how we must write the get_events()
function that returns at most five events:
#include <vector> std::vector<event> get_events();
The std::vector<event>
allocates memory, so the code from earlier is not a good solution.
#include <boost/array.hpp> boost::array<event, 5> get_events();
boost::array<event, 5>
does not allocate memory, but it constructs all the five elements. There's no way to return less than five elements.
#include <boost/container/small_vector.hpp> boost::container::small_vector<event, 5> get_events();
The boost::container::small_vector<event, 5>
does not allocate memory for five or less elements and allows us to return less than five elements. However, the solution is not perfect, because it is not obvious...