473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

User-defined manipulators

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
9 4532
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
"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
"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
"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
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
On 3¤ë22¤é, ¤W¤È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
"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

"weihan" <we****@math.ncu.edu.twwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
On 3¤ë22¤é, ¤W¤È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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

60
by: Fotios | last post by:
Hi guys, I have put together a flexible client-side user agent detector (written in js). I thought that some of you may find it useful. Code is here: http://fotios.cc/software/ua_detect.htm ...
3
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
6
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header"...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
7
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the...
0
by: tony | last post by:
Hello! This is a rather long mail but it's a very interesting one. I hope you read it. I have tried several times to get an answer to this mail but I have not get any answer saying something...
2
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be...
1
by: Carlettus | last post by:
Dear All, sorry but I'm not sure if this is the right place to post my problem. I was using the following asp code to create users in Active Directory. Suddenly, and I don't know the reason, users...
0
by: rbukkara | last post by:
Hi, I have got the following error while trying to add a user in the LDAP Directory. javax.naming.NameNotFoundException: ; remaining name 'uid=vassila,ou=People,dc=cs,dc=uno,dc=edu' I have...
9
by: Gordon | last post by:
I want to add a feature to a project I'm working on where i have multiple users set up on my Postgres database with varying levels of access. At the bare minimum there will be a login user who...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.