C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

overloaded operator<< and std::endl

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
sandor
Guest





PostPosted: Wed Jul 13, 2005 10:40 am    Post subject: overloaded operator<< and std::endl Reply with quote



This code does not compile with gcc 3, because std::endl is a template
method. How do I overload operator<< to accept any types including the
iostream manipulators, and endl?
-------------------
#include struct C
{
template <typename T>
void operator<< (T t) {}
};
int main()
{
C c;
c << "a";
c << std::endl;
return 0;
}

-hojtsy-


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Stephan Brönnimann
Guest





PostPosted: Thu Jul 14, 2005 8:10 am    Post subject: Re: overloaded operator<< and std::endl Reply with quote



sandor wrote:
Quote:
This code does not compile with gcc 3, because std::endl is a template
method. How do I overload operator<< to accept any types including the
iostream manipulators, and endl?
-------------------
#include struct C
{
template void operator<< (T t) {}
};

struct C {
// output operator for any type
template inline C& operator<<(
const T& t
);

// IO stream manipulators.
inline C& operator<<(
std::ostream& (*func)(std::ostream&)
);
inline C& operator<<(
std::ios& (*func)(std::ios&)
);
inline C& operator<<(
std::ios_base& (*func)(std::ios_base&)
);
};

Quote:
int main()
{
C c;
c << "a";
c << std::endl;
return 0;
}

[snip]


Stephan Brönnimann
[email]broeni (AT) osb-systems (DOT) com[/email]
Open source rating and billing engine for communication networks.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Tokyo Tomy
Guest





PostPosted: Sat Jul 16, 2005 10:21 am    Post subject: Re: overloaded operator<< and std::endl Reply with quote



"=?iso-8859-1?q?Stephan_Br=F6nnimann?=" <broeni (AT) hotmail (DOT) com> wrote

Quote:
sandor wrote:
This code does not compile with gcc 3, because std::endl is a template
method. How do I overload operator<< to accept any types including the
iostream manipulators, and endl?
-------------------
#include struct C
{
template void operator<< (T t) {}
};

struct C {
// output operator for any type
template inline C& operator<<(
const T& t
);

// IO stream manipulators.
inline C& operator<<(
std::ostream& (*func)(std::ostream&)
);
inline C& operator<<(
std::ios& (*func)(std::ios&)
);
inline C& operator<<(
std::ios_base& (*func)(std::ios_base&)
);
};

[snip]


I am not sure what the Op want to do by the struct C, but under the
following assumptions, I present a workable code below. The code is
inflexible, so if you need more flexibility, please think about
"Decorator" pattern of GOF or others.

Assumptions:

c << "Hello World" << std::endl;

print out to the cout as below

***** Hello World *****
[new line here]


[code]
#include

struct C {
// output operator for any type
template <typename T>
inline std::ostream& operator<<(const T& t)
{
return std::cout << "****** " << t << " ******";

}

// IO stream manipulators.
inline std::ostream& operator<<(
std::ostream& (*func)(std::ostream& )
){return std::cout << func;};
inline std::ostream& operator<<(
std::ios& (*func)(std::ios&)
){return std::cout << func;};
inline std::ostream& operator<<(
std::ios_base& (*func)(std::ios_base&)
){return std::cout << func;};
};



int main(int argc, char* argv[])
{
C c;
c << "Hello World" << std::endl;
c << std::endl;

return 0;
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Stephan Brönnimann
Guest





PostPosted: Sat Jul 16, 2005 11:40 pm    Post subject: Re: overloaded operator<< and std::endl Reply with quote



Tokyo Tomy schrieb:
[snip]
Quote:

I am not sure what the Op want to do by the struct C, but under the
following assumptions, I present a workable code below. The code is
inflexible, so if you need more flexibility, please think about
"Decorator" pattern of GOF or others.

No need for decorator patterns and the like, the logic of the output
operators and manipulators is good enough.

Quote:

Assumptions:

c << "Hello World" << std::endl;

print out to the cout as below

If you want to print to std::cout:
why define struct C in the first place?
See http://tinyurl.com/8toyu and related pages for a real life example
why you may need output to C.

[snip]

regards
Stephan Brönnimann
[email]broeni (AT) osb-systems (DOT) com[/email]
Open source rating and billing engine for communication networks.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Tokyo Tomy
Guest





PostPosted: Sun Jul 17, 2005 8:25 pm    Post subject: Re: overloaded operator<< and std::endl Reply with quote

"=?iso-8859-1?q?Stephan_Br=F6nnimann?=" <broeni (AT) hotmail (DOT) com> wrote

Quote:
Tokyo Tomy schrieb:
[snip]

I am not sure what the Op want to do by the struct C, but under the
following assumptions, I present a workable code below. The code is
inflexible, so if you need more flexibility, please think about
"Decorator" pattern of GOF or others.

No need for decorator patterns and the like, the logic of the output
operators and manipulators is good enough.


Assumptions:

c << "Hello World" << std::endl;

print out to the cout as below

If you want to print to std::cout:
why define struct C in the first place?

I wanted to give some pre-determined decorations to the output.

Quote:
See http://tinyurl.com/8toyu and related pages for a real life example
why you may need output to C.
[snip]

Thank you for your suggestion. I did not understand the reason of your
intention why the operator<< for a std: :manipulator returns C&, so
that I sent my code, expecting your response.

I thought that std::manipulators would do something to ostream, and
could do nothings to class C, which had no relationship to ostream.


Through the real life example, I certainly understand the
std::manipulators do nothing to class C and do something to ostream.
However nothing to do is very important for the application.


In the real life example, the operator<< looks like as below:

C& C::operator<<(std::ostream& (*func)(std::ostream&))
{
if (condition) return *this;
if(another_condition)
{
std::ostream* s = …
if(s) *s << func;
}
return *this;
}

Thank you for reminding me that nothing to do is important for some
applications.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Stephan Brönnimann
Guest





PostPosted: Mon Jul 18, 2005 8:20 am    Post subject: Re: overloaded operator<< and std::endl Reply with quote

Tokyo Tomy wrote:
Quote:
"=?iso-8859-1?q?Stephan_Br=F6nnimann?=" <broeni (AT) hotmail (DOT) com> wrote

Tokyo Tomy schrieb:
[snip]
See http://tinyurl.com/8toyu and related pages for a real life example
why you may need output to C.
[snip]

Thank you for your suggestion. I did not understand the reason of your
intention why the operator<< for a std: :manipulator returns C&, so
that I sent my code, expecting your response.

I thought that std::manipulators would do something to ostream, and
could do nothings to class C, which had no relationship to ostream.


Through the real life example, I certainly understand the
std::manipulators do nothing to class C and do something to ostream.
However nothing to do is very important for the application.


In the real life example, the operator<< looks like as below:

C& C::operator<<(std::ostream& (*func)(std::ostream&))
{
if (condition) return *this;

You forgot:
plog() << func;

Quote:
if(another_condition)
{
std::ostream* s = ...
if(s) *s << func;
}
return *this;
}

Thank you for reminding me that nothing to do is important for some
applications.


Where are going OT here, nonetheless:
The output operators of the class do not only work
on the output streams, but use data members of the class
(have a closer look at "condition", "another_condition" and "..."):

Stephan


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.