Previous section   Next section

Imperfect C++ Practical Solutions for Real-Life Programming
By Matthew Wilson
Table of Contents
Part Six.  Extending C++


Chapter 31. Return Value Lifetime

This chapter contains much of the material from a series of installments of Flexible C++, my C/C++ User's Journal Expert's Forum column [Wils2003d, Wils2003f, Wilson2004b]. It revolves around the seemingly trivial problem of converting integers to strings in an efficient manner.

As it turns out, it's anything but a trivial issue when the details start to come out, and as well as illustrating some practical ramifications of many of the issues covered in the book so far, it brings into sharp focus the issue of Return Value Lifetime (RVL) which we discussed in the section on Conversion Shims (see sections 16.2 and 20.5).

Probably the most common way to convert integers to strings is via sprintf(), whether called directly in client code or indirectly, for example, within IOStream insertion manipulators [Lang2000]. However, this very powerful function is overkill for many simple conversions, and there is a performance penalty for its use. We'll look at a simple, highly efficient, template-based technique, and then enhance it in several different forms, exploring the issues thrown up by each of five imperfect solutions.

Each of these solutions to the integer to string conversions is flawed in one way or another, and examination of these flaws will highlight issues within C++ that tend to bite the unwary (and sometimes the wary).


      Previous section   Next section