By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,330 Members | 1,590 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,330 IT Pros & Developers. It's quick & easy.

User-defined manipulators

P: n/a
A colleague wants to do this with a std::ostream:

os << fahrenheit << 37.0;

This would output 98.6 (i.e., Celsius --Fahrenheit).

I've already suggested os << fahrenheit(37.0), but he doesn't like it.

Then he suggested that the function taking the stream and fahrenheit manipulator
could return an instance of another user-defined type, to which the 37.0 is then
passed to do the conversion and output. This would work, but you might want to
pass something else, such as another manipulator, before the value (use a
catch-all member template maybe?).

Any other ideas?

DW
Mar 22 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On Mar 22, 9:36 am, "David W" <n...@email.providedwrote:
A colleague wants to do this with a std::ostream:

os << fahrenheit << 37.0;

This would output 98.6 (i.e., Celsius --Fahrenheit).

I've already suggested os << fahrenheit(37.0), but he doesn't like it.

Then he suggested that the function taking the stream and fahrenheit manipulator
could return an instance of another user-defined type, to which the 37.0 is then
passed to do the conversion and output. This would work, but you might want to
pass something else, such as another manipulator, before the value (use a
catch-all member template maybe?).

Any other ideas?

DW
Are you trying to redefine the meaning of operator <<? It is having an
associativity L->R. You may seek help of parenthesis to redifine the
meaning of operator.
e.g You may have to write like this after writing proper functions.
cout<<(f<<10);

Regards,
Sarath
http://sarathc.wordpress.com/

Mar 22 '07 #2

P: n/a
"Sarath" <CS*****@gmail.comwrote in message
news:11**********************@p15g2000hsd.googlegr oups.com...
On Mar 22, 9:36 am, "David W" <n...@email.providedwrote:
>A colleague wants to do this with a std::ostream:

os << fahrenheit << 37.0;

This would output 98.6 (i.e., Celsius --Fahrenheit).

I've already suggested os << fahrenheit(37.0), but he doesn't like it.

Then he suggested that the function taking the stream and fahrenheit
manipulator
could return an instance of another user-defined type, to which the 37.0 is
then
passed to do the conversion and output. This would work, but you might want
to
pass something else, such as another manipulator, before the value (use a
catch-all member template maybe?).

Any other ideas?

Are you trying to redefine the meaning of operator <<?
Not at all. You can already have, for example, os << std::setprecision(2) <<
std::setw(6) << 37.0;
'fahrenheit' would merely be an additional, user-defined, member of the family
to which setprecision and setw belong.
It is having an
associativity L->R. You may seek help of parenthesis to redifine the
meaning of operator.
e.g You may have to write like this after writing proper functions.
cout<<(f<<10);
DW
Mar 22 '07 #3

P: n/a
"David W" <no@email.providedwrote in message
news:u9******************@nasal.pacific.net.au...
"Sarath" <CS*****@gmail.comwrote in message
news:11**********************@p15g2000hsd.googlegr oups.com...
>On Mar 22, 9:36 am, "David W" <n...@email.providedwrote:
>>A colleague wants to do this with a std::ostream:

os << fahrenheit << 37.0;

This would output 98.6 (i.e., Celsius --Fahrenheit).

I've already suggested os << fahrenheit(37.0), but he doesn't like it.

Then he suggested that the function taking the stream and fahrenheit
manipulator
could return an instance of another user-defined type, to which the 37.0
is then
passed to do the conversion and output. This would work, but you might
want to
pass something else, such as another manipulator, before the value (use
a
catch-all member template maybe?).

Any other ideas?

Are you trying to redefine the meaning of operator <<?

Not at all. You can already have, for example, os << std::setprecision(2)
<< std::setw(6) << 37.0;
'fahrenheit' would merely be an additional, user-defined, member of the
family to which setprecision and setw belong.
>It is having an
associativity L->R. You may seek help of parenthesis to redifine the
meaning of operator.
e.g You may have to write like this after writing proper functions.
cout<<(f<<10);
Not that easy. I was playing around to see if I could figure it out. There
might be a way, but setw actually just calls a function in io_base to set
the hex then returns io_base. It actually doesn't process anythign
following.
Mar 22 '07 #4

P: n/a
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:EU**************@newsfe04.lga...
"David W" <no@email.providedwrote in message
>"Sarath" <CS*****@gmail.comwrote in message
>>Are you trying to redefine the meaning of operator <<?

Not at all. You can already have, for example, os << std::setprecision(2) <<
std::setw(6) << 37.0;
'fahrenheit' would merely be an additional, user-defined, member of the
family to which setprecision and setw belong.
>>It is having an
associativity L->R. You may seek help of parenthesis to redifine the
meaning of operator.
e.g You may have to write like this after writing proper functions.
cout<<(f<<10);

Not that easy. I was playing around to see if I could figure it out. There
might be a way, but setw actually just calls a function in io_base to set the
hex then returns io_base.
hex for setw?
It actually doesn't process anythign following.
Yes, that's the problem. The std:: manipulators alter the internal state of the
stream.

DW
Mar 22 '07 #5

P: n/a
On Thu, 22 Mar 2007 11:36:15 +1100 in comp.lang.c++, "David W"
<no@email.providedwrote,
>A colleague wants to do this with a std::ostream:

os << fahrenheit << 37.0;

This would output 98.6 (i.e., Celsius --Fahrenheit).

I've already suggested os << fahrenheit(37.0), but he doesn't like it.
You are right. He is wrong.

What, does he want every possible conversion formula to be remembered
inside iostreams? And does he want all the subsequent numerical output
to get scaled by some factor set by some code long ago and far away?

Mar 22 '07 #6

P: n/a
On 3月22日, 上午8時36分, "David W" <n...@email.providedwrote:
A colleague wants to do this with a std::ostream:

os << fahrenheit << 37.0;

This would output 98.6 (i.e., Celsius --Fahrenheit).

I've already suggested os << fahrenheit(37.0), but he doesn't like it.

Then he suggested that the function taking the stream and fahrenheit manipulator
could return an instance of another user-defined type, to which the 37.0 is then
passed to do the conversion and output. This would work, but you might want to
pass something else, such as another manipulator, before the value (use a
catch-all member template maybe?).

Any other ideas?

DW

Try this :

struct Fahrenheit {

ostream *fout ;

friend Fahrenheit& operator<< ( ostream& out , Fahrenheit& foo )
{
foo.fout = &out ;
return foo ;
}

};

ostream& operator<< ( const Fahrenheit& foo , double s ) {
return *(foo.fout) << 9*s/5+32 ;
}
Fahrenheit& operator<< ( Fahrenheit& foo , _Setw s ) {
foo.fout->width(s._M_n) ;
return foo ;
}
int main() {

Fahrenheit fahrenheit ;
int temp ;

for ( temp = 0 ; temp <= 100 ; temp+=10 )
cout << "C : " << setw(3) << temp << " -- F : "
<< fahrenheit << setw(3) << temp << endl ;

return 0 ;

}

Mar 22 '07 #7

P: n/a
"David Harmon" <so****@netcom.comwrote in message
news:46****************@news.west.earthlink.net...
On Thu, 22 Mar 2007 11:36:15 +1100 in comp.lang.c++, "David W"
<no@email.providedwrote,
>>A colleague wants to do this with a std::ostream:

os << fahrenheit << 37.0;

This would output 98.6 (i.e., Celsius --Fahrenheit).

I've already suggested os << fahrenheit(37.0), but he doesn't like it.

You are right. He is wrong.
He says that the form he prefers is more readable.
What, does he want every possible conversion formula to be remembered
inside iostreams?
I don't think any of them can be remembered inside iostreams. That's why I asked
if anyone had another idea. If there is another way, then maybe you could have
every possible conversion.
And does he want all the subsequent numerical output
to get scaled by some factor set by some code long ago and far away?
He probably wants it to work the same way as setw, which is only in effect for
the next insertion.

DW
Mar 22 '07 #8

P: n/a

"weihan" <we****@math.ncu.edu.twwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
On 3月22日, 上午8時36分, "David W" <n...@email.providedwrote:
A colleague wants to do this with a std::ostream:

os << fahrenheit << 37.0;

This would output 98.6 (i.e., Celsius --Fahrenheit).

I've already suggested os << fahrenheit(37.0), but he doesn't like it.

Then he suggested that the function taking the stream and fahrenheit
manipulator
could return an instance of another user-defined type, to which the 37.0 is
then
passed to do the conversion and output. This would work, but you might want to
pass something else, such as another manipulator, before the value (use a
catch-all member template maybe?).

Any other ideas?

Try this :

struct Fahrenheit {

ostream *fout ;

friend Fahrenheit& operator<< ( ostream& out , Fahrenheit& foo )
{
foo.fout = &out ;
return foo ;
}

};

ostream& operator<< ( const Fahrenheit& foo , double s ) {
return *(foo.fout) << 9*s/5+32 ;
}
Fahrenheit& operator<< ( Fahrenheit& foo , _Setw s ) {
foo.fout->width(s._M_n) ;
return foo ;
}
int main() {

Fahrenheit fahrenheit ;
int temp ;

for ( temp = 0 ; temp <= 100 ; temp+=10 )
cout << "C : " << setw(3) << temp << " -- F : "
<< fahrenheit << setw(3) << temp << endl ;

return 0 ;

}

Yes, that would work. The other manipulators would have to be included, though,
as well as anything else that's possible before the value is inserted (if there
is anything). It also wouldn't cater for manipulators that might be added in
future versions of the library. Still, he might have to live with something
that's not perfect (or go with my much simpler idea).

DW
Mar 23 '07 #9

P: n/a
"David W" <no@email.providedwrote in message
news:JC******************@nasal.pacific.net.au...
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:EU**************@newsfe04.lga...
>"David W" <no@email.providedwrote in message
>>"Sarath" <CS*****@gmail.comwrote in message
Are you trying to redefine the meaning of operator <<?

Not at all. You can already have, for example, os <<
std::setprecision(2) << std::setw(6) << 37.0;
'fahrenheit' would merely be an additional, user-defined, member of the
family to which setprecision and setw belong.

It is having an
associativity L->R. You may seek help of parenthesis to redifine the
meaning of operator.
e.g You may have to write like this after writing proper functions.
cout<<(f<<10);

Not that easy. I was playing around to see if I could figure it out.
There might be a way, but setw actually just calls a function in io_base
to set the hex then returns io_base.

hex for setw?
My bad, yeah, sayd setw then gave example for hex, but both do the same
thing. Set internal variables/switches.
>It actually doesn't process anythign following.

Yes, that's the problem. The std:: manipulators alter the internal state
of the stream.

Mar 24 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.