What do you consider good use of wrapper functions? When are they useful abstractions and in what cases harmful and unnecessary complexity?

3 Answers 3


Good uses for wrapper functions:

  1. Hiding the complexity of a horribly complex or unsafe, low-level API, for example, the Win32 API. However, your wrapper must actually reduce complexity and/or increase safety for the common cases.

  2. Making something cross-platform when you are writing a generic library or have a good reason to believe it needs to be cross-platform.

Bad uses:

  1. Because we can.

  2. Because maybe, at some point, we might switch to a new database in our quick n' dirty app we're writing.

  3. Because I personally don't like the API for some bikeshed reason even though the wrapper isn't actually hiding any complexity.


To expand on @dsimcha answer (just giving examples) I usually wrap a number of C++ STL algorithms, simply because they are all designed in term of iterators, but I find myself calling them over whole collection most of the time.


template <typename RndIt>
void sort(RndIt begin, RndIt end);


template <typename Container>
void sort(Container& c) { std::sort(c.begin(), c.end()); }

Small gain, but still:

  • I cannot mess up and pass end first and begin second
  • It makes caller code more readable

It can also help introducing sanity checks, for example the includes algorithm can only be called if both collection are sorted...

template <typename Big, typename Small>
bool includes(Big const& big, Small const& small) {
  assert(std::is_sorted(big.begin(), big.end()) && "big not sorted");
  assert(std::is_sorted(small.begin(), small.end()) && "small not sorted");
  return std::includes(big.begin(), big.end(), small.begin(), small.end());

Of course using the STL in debug mode would probably perform this check, depending on the implementation... or so I can hope... but I'd rather make sure :)

And there is, finally, the embodiement of simple idioms. For example, erasing elements according to a predicate in C++ can be done using:

c.erase(std::remove_if(c.begin(), c.end(), pred), c.end());

This can be wrapped up in:

template <typename Container, typename Pred>
void erase_if(Container& c, Pred pred);

to simply caller code, and once again make sure it does not mess up the arguments/idiom.

  • With your fancy wrappers you seem to just duplicate what is already in boost::range. Mar 29, 2012 at 10:56
  • @honk: Which I guess support my propos: wrapping is useful ;) I like Boost.Range, however it does not add the "debug" guarantees. Mar 29, 2012 at 11:41

Just look at any template from the Spring framework

That is good wrapping in action.

They have taken all the common use cases for a wide variety of standard JEE technologies (JPA, JMS, JAX-RS, JAXB etc) and made a neat set of templates that act as simple base classes to extend from. These template classes greatly simplify the simple use cases of the libraries and allow developers to get the basics done really easily. Once you've found your feet with the required technology, then you can explore the other base classes provided which wrap the more sophisticated classes.

What makes them so good is that they all follow a very similar approach which means that once you've got the hang of one template then you have a good head start on the next.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.