GNU autosprintf, version 1.0
Formatted Output to Strings in C++
Table of Contents
This package makes the C formatted output routines (
fprintf et al.)
usable in C++ programs, for use with the
<string> strings and the
It allows to write code like
cerr << autosprintf ("syntax error in %s:%d: %s", filename, line, errstring);
cerr << "syntax error in " << filename << ":" << line << ": " << errstring;
The benefits of the autosprintf syntax are:
It reuses the standard POSIX printf facility. Easy migration from C to C++.
English sentences are kept together.
It makes internationalization possible. Internationalization requires format
strings, because in some cases the translator needs to change the order of a
sentence, and more generally it is easier for the translator to work with a
single string for a sentence than with multiple string pieces.
It reduces the risk of programming errors due to forgotten state in the
output stream (e.g.
cout << hex; not followed by
cout << dec;).
An instance of class
autosprintf just contains a string with the
formatted output result. Such an instance is usually allocated as an
automatic storage variable, i.e. on the stack, not with
new on the
autosprintf (const char *format, ...) takes a format
string and additional arguments, like the C function
char * and
std::string are defined that return
the encapsulated string.
~autosprintf () destroys the encapsulated string.
operator << is provided that outputs the encapsulated string to the
To use the
autosprintf class in your programs, you need to add
to your source code.
The include file defines the class
autosprintf, in a namespace called
gnu. The `using´ statement makes it possible to use the class
without the (otherwise natural)
When linking your program, you need to link with
that's where the class is defined. In projects using GNU
this means adding `AC_LIB_LINKFLAGS([asprintf])´ to
configure.ac, and using the @LIBASPRINTF@ Makefile variable that
This document was generated on 12 June 2003 using the
translator version 1.52a.