Topic: proposal: format function for ostream


Author: vadima@waterint.com (VAdim)
Date: Fri, 28 Feb 2003 21:39:35 +0000 (UTC)
Raw View
HI,

I have the implementation for that function for myself.
It something like yours, but less nice.

Goal of this function is to simplify formatted output

converting

printf( "%8.3f %5.2f %4d", f, g, d );

will be VERY ugly in streams.

Sincerely Yours,
Vadim

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: richard@ex-parrot.com ("Richard Smith")
Date: Mon, 3 Mar 2003 03:38:51 +0000 (UTC)
Raw View
"VAdim" <vadima@waterint.com> wrote in message
news:ac9acec7.0302281338.3c916662@posting.google.com...

> Goal of this function is to simplify formatted output
>
> converting
>
> printf( "%8.3f %5.2f %4d", f, g, d );
>
> will be VERY ugly in streams.

There's always boost::format.  This allows you to write

  std::cout << boost::format( "%8.3f %5.2f %d" ) % f % g % d;

If you consider the stdio version to be less ugly than the iostream version,
you'd probably like this.  Personally, I've always found the iostream
versions less ugly, but perhaps that is because I rarely write heavily
numerical code, and so don't often find myself concerned with the
precisions.

--
Richard Smith


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: math@antiquark.com (Derek Ross)
Date: Mon, 3 Mar 2003 03:38:56 +0000 (UTC)
Raw View
The boost website has a "format" library that emulates much of the
printf formatting. More info here:

http://www.boost.org/libs/format/doc/format.html


VAdim wrote:
> HI,
>
> I have the implementation for that function for myself.
> It something like yours, but less nice.
>
> Goal of this function is to simplify formatted output
>
> converting
>
> printf( "%8.3f %5.2f %4d", f, g, d );
>
> will be VERY ugly in streams.
>
> Sincerely Yours,
> Vadim
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]
>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: vadima@waterint.com (VAdim)
Date: Tue, 25 Feb 2003 16:16:32 CST
Raw View
Hi All,

My proposal is to include simple format function into std::

instead:

double f;

cout << setw( 8 ) << precision(3) << f << endl;

I'd prefer:

cout << fmt( f, 8, 3 ) << endl;

that function fmt( T& f, new_width, new_precision = 0 )
 save current width/precision;
 if new_width !=0 set new width;
 if new_precision > 0 set new precision;
 out f;
 restore width/precision;
}

Sincerely Yours,
Vadim

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: richard@ex-parrot.com ("Richard Smith")
Date: Wed, 26 Feb 2003 22:08:11 +0000 (UTC)
Raw View
"VAdim" <vadima@waterint.com> wrote in message
news:ac9acec7.0302251410.ee4839e@posting.google.com...

> instead:
>
> double f;
>
> cout << setw( 8 ) << precision(3) << f << endl;

setprecision, surely?

> I'd prefer:
>
> cout << fmt( f, 8, 3 ) << endl;

There's nothing to stop you writing your own version, outside of the std
namespace (see below), but I'm not sure that this achieves anything other
than an unecessary change of syntax.  Also, I'm not sure that the handling
of the zero widths and precisions is ideal as you've specified them.  (Your
specification means that

  cout << setw(5) << fmt(f,3) << g << endl;

is equivalent to

  cout << setw(3) << f << setw(5) << g << endl;

which seems wrong.)

  #include <iostream>
  #include <iomanip>

  template <typename T>
  class fmt_t
  {
   public:
    fmt_t( T const& t, int width, int precision = 0 )
      : t(t), width(width), precision(precision)
    {}

    template <typename CharT, class Traits>
    friend std::basic_ostream<CharT, Traits>&
    operator<<( std::basic_ostream<CharT, Traits>& os,
                fmt_t<T> const& fmt )
    {
      class state
      {
       public:
        state( std::ios_base& str )
          : str(str), width(str.width()), precision(str.precision())
        {}
       ~state() { str.precision(precision); str.width(width); }

       private:
        std::ios_base& str;
        int width, precision;

      } stored_state(os);

      if (fmt.width)     os.width    (fmt.width);
      if (fmt.precision) os.precision(fmt.precision);

      return os << fmt.t;
    }

   private:
    T t;
    int width, precision;
  };

  template <class T>
  fmt_t<T> fmt( const T &t, int width, int prec = 0 )
  {
    return fmt_t<T>(t, width, prec);
  }


  int main()
  {
    double f( 3.14 );
    std::cout << fmt( f, 8, 3 ) << std::endl;
  }

(The enthusiastic might want to modify this so that it compiles in something
other than Comeau ;-).)

--
Richard Smith


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]