Boost logo

Boost :

From: Alexander Nasonov (alnsn_at_[hidden])
Date: 2002-08-28 11:39:31

Dowload it from:

I know how hot this topic is :) I already tried it before.
I like my new versions much more then previous. The format
function is even more simple:

struct arg;

format_ret format(const char * fmt, const arg & /* up to ten */ );
// format_ret has operator std::string()
// operator<<(std::ostream &, format_ret)
// and operator()(const arg & /* up to ten */ )

template<typename T>
void write_arg(std::ostream &, const void *);

struct arg
  template<typename T>
  arg(const T & val)
    : write_func_(&write_arg<T>)
    , ptr_(&val) {}

  const void * ptr_;
  void (*write_func_)(std::ostream &, const void *);

I think that the presense of ctor templates in C++ can be used to
generalize functions with ellipsis (and they will really usefull):

string format(const char * str, arg first ... last)
  // [first, last] range is like C array
  arg * begin = &first;
  arg * end = &last + 1;
  format(str, begin, end);

string s = format("{1} + {2} = {3}", '1', 2, 3);

I should think about that and then post proposal to comp.std.c++.

Cut from WHAT'S THIS section of README file:

This is a first (very basic) version of printf-like library. Funny, but
I would better say prinf-unlike because unlike printf it is type safe and
again unlike printf it has completely different format (no %s, %d, ...).
This library also supports reordering. More precisely, the first version
supports only reordering. Formatting facilities are left to future versions.
Formal documentation does not exist for the moment so I'll try to explain
features on examples:

1. hello world application using intermediate string

  string say_hello = format("{1}, {2}{3}", "Hello", "world", '!');
  cout << say_hello << endl;

2. or directly to stream:

  cout << format("{1}, {2}{3}", "Hello", "world", '!') << endl;

3. for more then 10 parameters

  const char * fmt = "{15}*{15}={2}{2}{5}";
  wstring str = format(fmt, 1, 2, 3, 4, 5, 6, 7, 8, 9)(10, 11, 12, 13, 14,

The library is very fast. Format of 20 variables is 1.07-1.1 slower then
them directly to ostringstream. There is no new/delete code in the library.
Moreover, if string::const_iterator is typedef for const char * then putting
format return directly to the stream does not involve any dynamic memory
allocations in library code (underlying stream can allocate dynamic memory,
course). Instead, library extensively uses temporaries and based of fact
the temporaries are destroyed at the end of full expression.
I have many future plans. Of course, I'm going to add formatting flags:
  cout << format("{1:02x}", 137) << endl;
May be locales:
  cout << format("default: {1}, russian: {1/ru}", get_current_date());
And may be named parameters:
  named_param p_pi("pi");
  double pi = get_pi();
  cout << format("pi = {pi}\n", p_pi = pi);

Best regards,
Alexander Nasonov
e-mail account: alnsn
e-mail server:

Boost list run by bdawes at, gregod at, cpdaniel at, john at