vul_sprintf.h
Go to the documentation of this file.
1 // This is core/vul/vul_sprintf.h
2 #ifndef vul_sprintf_h_
3 #define vul_sprintf_h_
4 //:
5 // \file
6 // \brief creates a formatted ANSI C++ string
7 // \author Andrew W. Fitzgibbon, Oxford RRG
8 // \date 08 Aug 96
9 //
10 // \verbatim
11 // Modifications
12 // 10 June 1999 fsm removed constructor from 'const std::string &' and
13 // changed remaining constructors to use do_vul_sprintf().
14 // Peter Vanroose 27/05/2001: Corrected the documentation
15 // \endverbatim
16 //-----------------------------------------------------------------------------
17 
18 #include <string>
19 #include <iosfwd>
20 #ifdef _MSC_VER
21 # include <vcl_msvc_warnings.h>
22 #endif
23 
24 //: C++ conforming replacement to the ANSI C functions sprintf and printf.
25 // vul_sprintf works in the same way as sprintf but is itself an ANSI C++ string
26 // which can either be kept or output directly using streams e.g.
27 // \code
28 // std::cerr << vul_sprintf("int %d, float %f ", 1, 3.14)
29 // << bigobject << std::endl;
30 // \endcode
31 
32 struct vul_sprintf : public std::string
33 {
34  // ISO C++ does not allow reference types or structure types for the
35  // argument preceding ... in a function taking a variable number of
36  // parameters.
37  // So we can't have any of these constructors:
38  // vul_sprintf(std::string const& fmt, ...);
39  // vul_sprintf(std::string fmt, ...);
40  vul_sprintf(char const *fmt, ...);
41 
42 #ifndef _WIN32
43  // assignment
44  vul_sprintf& operator=(std::string const& s)
45  { std::string::operator=(s); return *this; }
46  vul_sprintf& operator=(char const* s)
47  { std::string::operator=(s); return *this; }
48 #endif
49 
50  operator char const* () const { return c_str(); }
51 };
52 
53 std::ostream& operator<<(std::ostream &os, const vul_sprintf& s);
54 
55 #endif // vul_sprintf_h_
std::ostream & operator<<(std::ostream &os, const vul_sprintf &s)
Definition: vul_sprintf.cxx:33
vul_sprintf & operator=(std::string const &s)
Definition: vul_sprintf.h:43
vul_sprintf(char const *fmt,...)
Definition: vul_sprintf.cxx:19
C++ conforming replacement to the ANSI C functions sprintf and printf.
Definition: vul_sprintf.h:31