Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #7506
| From | Ruben Safir <ruben@mrbrklyn.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | iostream manipulator construction and design |
| Date | 2011-07-06 02:55 +0000 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <iv0iqd$alg$1@reader1.panix.com> (permalink) |
I've covered the iostream operations in Stroustrup but there is a section
that I'm failing to understand in section 21.4.6.1 (of the third edition
of C++ - The Programming Language) which I hope that someone can help
me decipher. The section discusses Manipultors that take arguments and
Stroustrup states
<QUOTE>
"Manipulators that take arguments can also be useful. For example, we
might want to write
cout << setprecision(4) << angle;
to print the value of the floating-point variable angle with four
digits.
To do this, setprecision must return an object that is initialized by 4
and that calls cout::setprcision when involked. Such a manipulator is a
function object thatr is involed by << rather than by (). The exact
tyoe of function object is implementation-defined, but it might be
defined like this:
struct smanip{
ios_base& (*f)(ios_base&, int); //function to be called
int i;
smanip(ios_base& (*ff) (ios_base&, int), int ii) : f(ff, i(ii){}
};
template<class Ch, class Tr>
ostream<Ch, Tr>& operator<<(ostream<Ch, Tr>& os, smanip& m)
{
return m.f(os, m.i);
}
The smanip constructor stores its arguments in f and i and the
operator<< calls f(i). We can now define setprecision() like this:
ios_base& set_precision(ios)base& s, int n) //helper
{
return s.setprecision(n); //call member function
}
inline smap setprecision(int n)
{
return smanip(set_precision, n);
}
We can now write:
cout << setprecision(4) << angle;
"
</QUOTE>
What don't understand a couple of things here, and this might be an
accumulation of increasing confusion on my part as I've continued
through this chapter.
First, I just don't understand what the problem is that is being solved.
Why can't a manipulator just take an argument of an int and return a
base_stream object, like the manipulators that don't take an argument?
Secondly, the exqmple completely befuddles me. I'm guessing that don't
understand the order of operations of
cout << setprecision(4) << angle;
is ostream<< called first and then the function setprecision?
Back to comp.lang.c++ | Previous | Next — Next in thread | Find similar | Unroll thread
iostream manipulator construction and design Ruben Safir <ruben@mrbrklyn.com> - 2011-07-06 02:55 +0000
Re: iostream manipulator construction and design Michael DOUBEZ <michael.doubez@free.fr> - 2011-07-06 01:00 -0700
Re: iostream manipulator construction and design Ruben Safir <ruben@mrbrklyn.com> - 2011-07-06 15:01 +0000
Re: iostream manipulator construction and design "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-06 19:13 +0200
Re: iostream manipulator construction and design Ruben Safir <ruben@mrbrklyn.com> - 2011-07-06 18:00 +0000
Re: iostream manipulator construction and design Ruben Safir <ruben@mrbrklyn.com> - 2011-07-07 02:51 +0000
Re: iostream manipulator construction and design "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-07 11:13 +0200
csiph-web