Boost logo

Boost :

From: Terje Slettebø (tslettebo_at_[hidden])
Date: 2003-03-28 21:39:47


>From: "Jason House" <jhouse_at_[hidden]>

> Terje Slettebø wrote:
> >
> > >From: "Jason House" <jhouse_at_[hidden]>
> >
> > > 1. Is it at all useful/possible to use a lambda-like notation?
> >
> Thinking about it more, I don't know how doable it is for the first 3
> parameters...
>
> But what about:
>
> /* brain dead function to serve as example */
> std::string printable(char z) {return z=0?"\0":std::string(z);}
>
> char x[7] = "foobar";
> std::cout
> << io_format<char[N]>("{", "}", ", ", "'" << printable(_1) << "'")
> << "x = " << x;
>
> output:
> x = {'f', 'o', 'o', 'b', 'a', 'r', '\0'}
>
> alternates like (printable(_1) or just "'" << _1 << "'" would also work.
>
> That makes the visitor possible, but also makes the inspiring case for
> the extra inputs to io_format be a little more readable...

This still wouldn't, in itself, support things like outputting indices,
though, unless the entity being passed may hold state, such as a function
object.

It also looks like quite a bit more complex, apparently much of it just to
be able to write "\'" << _1 << "\'" instead of "\'","\'".

There's another issue: The type of the lambda expression would likely encode
the expression, meaning you have an issue of how to store and retrieve it,
unless you know the exact type of the object to retrieve.

This is the code to retrieve the format_data object for a given type (given
with "index"):

template<class CharTraits>
static format_data<CharType> &format(std::basic_ostream<CharType,CharTraits>
&stream)
{
  void *&void_ptr=stream.pword(index);

  if(!void_ptr)
    void_ptr=new format_data<CharType>(default_data);

  return *static_cast<format_data<CharType> *>(void_ptr);
}

How to do that, if the object being retrieved is an arbitrary type?

Regards,

Terje


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk