You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@stdcxx.apache.org by Ravi Inampudi <Ra...@roguewave.com> on 2008/01/31 00:07:57 UTC

"omanip" in C++ standard library

There is no template for "omanip" in C++ standard library. What are the
options for people migrating to C++ standard library?

Ravi

Re: "omanip" in C++ standard library

Posted by Travis Vitek <vi...@roguewave.com>.


Travis Vitek wrote:
> 
> 
> 
> Ravi Inampudi-2 wrote:
>> 
>> There is no template for "omanip" in C++ standard library.
>> 
> 
> I'm pretty sure that omanip was something from the old iostreams
> implementation. It is not in the standard.
> 
> 
> Ravi Inampudi-2 wrote:
>> 
>> What are the options for people migrating to C++ standard library?
>> 
> 
> The option is to write out the code explicitly. The model would be
> something like this...
> 
>     #include <iostream>
> 
>     template <class CharT>
>     struct repeat_n_impl {
>       repeat_n_impl (CharT c, size_t n)
>         : c_ (c), n_ (n)
>       {
>       }
> 
>       CharT c_;
>       size_t n_;
>     };
> 
>     template <class CharT, class TraitsT>
>     std::basic_ostream<CharT, TraitsT>& operator<< (
>         std::basic_ostream<CharT, TraitsT>& os,
>         const repeat_n_impl<CharT>& rep)
>     {
>       for (size_t n = 0; n < rep.n_; ++n)
>           os << rep.c_;
>       return os;
>     }
> 
>     template <class CharT>
>     repeat_n_impl<CharT> repeat_n (CharT c, size_t n) {
>       return repeat_n_impl<CharT>(c, n);
>     }
> 
>     int main ()
>     {
>         std::cout << repeat_n ('a', 100) << std::endl;
>         return 0;
>     }
> 
> Another option would be to implement something like the old omanip
> template. Unfortunately I only think that omanip worked for one additional
> parameter, so it wouldn't work with the above example. I believe that the
> following is similar...
> 
>     template <class T>
>     struct omanip
>     {
>         typedef std::ostream& (*omanip_function)(std::ostream&, T);
> 
>         omanip (omanip_function fn, T val)
>             : fn_ (fn), val_ (val)
>         {
>         }
> 
>         friend std::ostream& operator<< (std::ostream& os, const omanip&
> om)
>         {
>             return om.fn_ (os, om.val_);
>         }
> 
>         omanip_function fn_;
>         T val_;
>     };
> 
> Travis
> 

Another disadvantage of this is that it doesn't work with other stream
types. I.e. it doesn't work with std::wostream. You could make some
modifications, but the syntax that the user would need to write would
change.

Travis

-- 
View this message in context: http://www.nabble.com/%22omanip%22-in-C%2B%2B-standard-library-tp15194916p15195204.html
Sent from the stdcxx-dev mailing list archive at Nabble.com.


Re: "omanip" in C++ standard library

Posted by Travis Vitek <vi...@roguewave.com>.


Ravi Inampudi-2 wrote:
> 
> There is no template for "omanip" in C++ standard library.
> 

I'm pretty sure that omanip was something from the old iostreams
implementation. It is not in the standard.


Ravi Inampudi-2 wrote:
> 
> What are the options for people migrating to C++ standard library?
> 

The option is to write out the code explicitly. The model would be something
like this...

    #include <iostream>

    template <class CharT>
    struct repeat_n_impl {
      repeat_n_impl (CharT c, size_t n)
        : c_ (c), n_ (n)
      {
      }

      CharT c_;
      size_t n_;
    };

    template <class CharT, class TraitsT>
    std::basic_ostream<CharT, TraitsT>& operator<< (
        std::basic_ostream<CharT, TraitsT>& os,
        const repeat_n_impl<CharT>& rep)
    {
      for (size_t n = 0; n < rep.n_; ++n)
          os << rep.c_;
      return os;
    }

    template <class CharT>
    repeat_n_impl<CharT> repeat_n (CharT c, size_t n) {
      return repeat_n_impl<CharT>(c, n);
    }

    int main ()
    {
        std::cout << repeat_n ('a', 100) << std::endl;
        return 0;
    }

Another option would be to implement something like the old omanip template.
Unfortunately I only think that omanip worked for one additional parameter,
so it wouldn't work with the above example. I believe that the following is
similar...

    template <class T>
    struct omanip
    {
        typedef std::ostream& (*omanip_function)(std::ostream&, T);

        omanip (omanip_function fn, T val)
            : fn_ (fn), val_ (val)
        {
        }

        friend std::ostream& operator<< (std::ostream& os, const omanip& om)
        {
            return om.fn_ (os, om.val_);
        }

        omanip_function fn_;
        T val_;
    };

Travis
-- 
View this message in context: http://www.nabble.com/%22omanip%22-in-C%2B%2B-standard-library-tp15194916p15195203.html
Sent from the stdcxx-dev mailing list archive at Nabble.com.


Re: "omanip" in C++ standard library

Posted by Martin Sebor <se...@roguewave.com>.
Ravi Inampudi wrote:
> There is no template for "omanip" in C++ standard library. What are the
> options for people migrating to C++ standard library?

What exactly is this omanip supposed to do?

Is it something like this:
http://h30097.www3.hp.com/cplus/clu007.htm#index_x_247

There is no feature like that in the standard but the implementation
defined "smanip" type might be close. It isn't difficult to write one's
own, either using the implementation-defined "smanip" type or completely
from scratch. We have a few examples demonstrating how:

http://svn.apache.org/repos/asf/stdcxx/tags/4.2.0/examples/manual/fmtflags_manip.cpp
http://svn.apache.org/repos/asf/stdcxx/tags/4.2.0/examples/manual/money_manip.cpp
http://svn.apache.org/repos/asf/stdcxx/tags/4.2.0/examples/manual/time_manip.cpp

The first one was prompted by a discussion in the following thread:
http://groups.google.com/group/comp.lang.c++/browse_frm/thread/acfa77336a6137cd/50e116d0a0c889ad

Martin

> 
> Ravi
>