jeudi 5 mars 2015

Writing a modern function interface to "produce a populated container"

When I cut my teeth on C++03, I saw several popular approaches to writing a function that produces a populated container (A "give me the collection of things" function):



template< typename Container >
void make_collection( std::insert_iterator<Container> );



or:



void make_collection( std::vector<Thing> & );



  • This is not container agnostic

  • The interface doesn't communicate that an empty container is expected.


or:



std::vector<Thing> make_collection();



  • This is not container agnostic

  • There are several avenues for unnecessary copying.


Using modern C++ standards, is there a more idiomatic function interface to "produce a populated container"?


Aucun commentaire:

Enregistrer un commentaire