Boost logo

Boost :

From: Jason House (jhouse_at_[hidden])
Date: 2003-03-17 10:12:19

Terje Slettebø wrote:
> typedef std::vector<char> vector_char;
> vector_char values;
> // Fill with 'A', 'B', 'C'
> std::cout << io::format<vector_char>("[", "]", ", ", "\'", "\'") << values;
> Output:
> ['A', 'B', 'C']
> However, is this overkill?

  It seems that way, especially considering that if it was a vector of
anything other than a fundamental type with std::cout << already defined
for it, you could add a second io::format<char>("\'","\'","") and it
would work.
> Feedback is most welcome.

  Would possibly a function object for outputting the contained item be
better? Here's a possibly mutilated example (I've never actually used
function objects before):

std::ostream delimitted_char(std::ostream out, char x, std::string
first, std::string last);

std::cout << io::format<vector_char>("[", "]", ", ",
  bind("\'", 4, bind("\'", 3, delimitted_char)))

Of course, the function object parameter should have a nice default to
the << operator.

> > > There was a suggestion for allowing generic formats, though, using
> > > the same format for all instantiations of a template. The question is
> how to
> > > do it. The current version stores the format for each specific type, as
> you
> > > say. Volodya suggested a couple of ways it could be done, partial
> > > specialisation, or storing the template name in a map.
> There's yet another alternative way this may be done, using "placeholder"
> types, i.e.:
> std::cout << io::format<std::vector<_> >(...); // Sets the format for all
> vectors
> std::cout << io::format<_>(...); // Sets the format for all types (defaults)
> This would avoid hardcoding any defaults, as the user could change it.
> The output routines could then check the formats in the following order,
> e.g. for "std::vector<char>":
> If there's a format set for std::vector<char>, use it, else
> if there's a format set for std::vector<_> (all vectors), use it, else
> use format for _.
> Comments?

I like it :)

Using just _ scares me a bit... It would have to have a good default :)
The result would be intuitive, so I guess a use at your own risk is ok.
In some ways, maybe it's good that items like char wouldn't use it :)

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