473,322 Members | 1,690 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,322 software developers and data experts.

Compiler warning on ostream operator,

The code below should allow to use a comma instead of << with ostreams and
include a space between two operands when comma is used. e.g.

cout << "hello", "world", endl;

should print the line "hello world".
#include <iostream>
using namespace std;

inline std::ostream& operator,(std::ostream& rhs_, std::ostream& (* arg_
(std::ostream&))
{ return (rhs_ << arg_);
}
template<class Tstd::ostream& operator,(std::ostream& rhs_, T arg_) {
return (rhs_ << ' ' << arg_); }

int main()
{ cout << "hello", "world", endl;
}
When I compile with the Weffc++ option, i get the warnings:

junk.cpp:5: Warning: user-defined »std::ostream& operator,(std::ostream&,
std::ostream& (*)(std::ostream&))« always evaluates both arguments
junk.cpp:9: Warning: user-defined »std::ostream& operator,(std::ostream&,
T)« always evaluates both arguments
Why? I mean, of course the operator evaluates both arguments, that's what
they are for. BTW the code works fine; I'm just confused by these warnings.
I didn't find anything in the effective c++ books that could throw some
light on this. Can you?

Thanks,
Thomas

Oct 1 '08 #1
3 2396

"Thomas Lenz" <le****@gmx.dewrote in message
news:48**********************@newsspool3.arcor-online.net...
The code below should allow to use a comma instead of << with ostreams and
include a space between two operands when comma is used. e.g.

cout << "hello", "world", endl;

should print the line "hello world".
#include <iostream>
using namespace std;

inline std::ostream& operator,(std::ostream& rhs_, std::ostream& (* arg_
(std::ostream&))
{ return (rhs_ << arg_);
}
template<class Tstd::ostream& operator,(std::ostream& rhs_, T arg_) {
return (rhs_ << ' ' << arg_); }

int main()
{ cout << "hello", "world", endl;
}
When I compile with the Weffc++ option, i get the warnings:

junk.cpp:5: Warning: user-defined »std::ostream& operator,(std::ostream&,
std::ostream& (*)(std::ostream&))« always evaluates both arguments
junk.cpp:9: Warning: user-defined »std::ostream& operator,(std::ostream&,
T)« always evaluates both arguments
Why? I mean, of course the operator evaluates both arguments, that's what
they are for. BTW the code works fine; I'm just confused by these
warnings.
I didn't find anything in the effective c++ books that could throw some
light on this. Can you?

Thanks,
Thomas
I think you should look up the comma (',') operator.

Bill
Oct 1 '08 #2
On Oct 1, 7:44 am, Thomas Lenz <len...@gmx.dewrote:
The code below should allow to use a comma instead of << with
ostreams and include a space between two operands when comma
is used. e.g.
cout << "hello", "world", endl;
should print the line "hello world".
Why? Have you overloaded the comma operator to do this? If so,
it's obfuscation; the classical idiom is:

std::cout << "hello" << ' ' << "world" << std::endl ;

And when there's a classical idiom, overloading operators to use
a different syntax is obfuscation in the first degree.
#include <iostream>
using namespace std;
inline std::ostream& operator,(std::ostream& rhs_, std::ostream& (* arg_
(std::ostream&))
{ return (rhs_ << arg_);}
template<class Tstd::ostream& operator,(std::ostream& rhs_, T arg_) {
return (rhs_ << ' ' << arg_); }
int main()
{ cout << "hello", "world", endl;
}
When I compile with the Weffc++ option, i get the warnings:
junk.cpp:5: Warning: user-defined »std::ostream& operator,(std::ostream&,
std::ostream& (*)(std::ostream&))« always evaluates both arguments
junk.cpp:9: Warning: user-defined »std::ostream& operator,(std::ostream&,
T)« always evaluates both arguments
Why?
Well, the message is confusing. Of course, the operator, always
evaluates both arguments. The message looks more like it was
meant for && and || (where user defined operators always
evaluate both arguments, but the built-in operators
short-circuit). But overloading the comma operator like this is
*not* a good idea. Overloading the comma operator in general is
not a good idea (and is forbidden in most coding guidelines).
I mean, of course the operator evaluates both arguments,
that's what they are for. BTW the code works fine; I'm just
confused by these warnings. I didn't find anything in the
effective c++ books that could throw some light on this. Can
you?
Well, if the message had said something about the operator not
introducing a sequence point (when the built-in operator does),
it would make sense. Or simply if it said that this could
silently change the meaning of working code.

One of the rare legitimate uses I can think of the comma
operator is for conditionally inserting debugging output, in
conjunction with macros. Something like:

#ifdef ACTIVATE_DEBUGGING
#define TRACE( x ) std::cerr << x
#else
#define TRACE( x ) (void)0
#endif

class C
{
public:
C( int i ) : myValue( TRACE( i ), i ) {}
// ...
} ;

If your operator is defined before this header is included,
you're going to get some very surprising results when tracing is
turned on.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 1 '08 #3
am Mittwoch 01 Oktober 2008 11:48 schrieb James Kanze:
On Oct 1, 7:44 am, Thomas Lenz <len...@gmx.dewrote:
>The code below should allow to use a comma instead of << with
ostreams and include a space between two operands when comma
is used. e.g.
> cout << "hello", "world", endl;
>should print the line "hello world".

Why? Have you overloaded the comma operator to do this? If so,
it's obfuscation; the classical idiom is:

std::cout << "hello" << ' ' << "world" << std::endl ;

And when there's a classical idiom, overloading operators to use
a different syntax is obfuscation in the first degree.

It's minimized code to highlight my comma operator question. I'm not going
to do this in real life.

>
>#include <iostream>
using namespace std;
>inline std::ostream& operator,(std::ostream& rhs_, std::ostream& (* arg_
(std::ostream&))
{ return (rhs_ << arg_);}
>template<class Tstd::ostream& operator,(std::ostream& rhs_, T arg_) {
return (rhs_ << ' ' << arg_); }
>int main()
{ cout << "hello", "world", endl;
}
>When I compile with the Weffc++ option, i get the warnings:
>junk.cpp:5: Warning: user-defined »std::ostream& operator,(std::ostream&,
std::ostream& (*)(std::ostream&))« always evaluates both arguments
junk.cpp:9: Warning: user-defined »std::ostream& operator,(std::ostream&,
T)« always evaluates both arguments
>Why?

Well, the message is confusing. Of course, the operator, always
evaluates both arguments. The message looks more like it was
meant for && and || (where user defined operators always
evaluate both arguments, but the built-in operators
short-circuit).

That's a very plausible explanation. Thank you for pointing this out.

But overloading the comma operator like this is
*not* a good idea. Overloading the comma operator in general is
not a good idea (and is forbidden in most coding guidelines).
>I mean, of course the operator evaluates both arguments,
that's what they are for. BTW the code works fine; I'm just
confused by these warnings. I didn't find anything in the
effective c++ books that could throw some light on this. Can
you?

Well, if the message had said something about the operator not
introducing a sequence point (when the built-in operator does),
it would make sense. Or simply if it said that this could
silently change the meaning of working code.

One of the rare legitimate uses I can think of the comma
operator is for conditionally inserting debugging output, in
conjunction with macros. Something like:

#ifdef ACTIVATE_DEBUGGING
#define TRACE( x ) std::cerr << x
#else
#define TRACE( x ) (void)0
#endif

class C
{
public:
C( int i ) : myValue( TRACE( i ), i ) {}
// ...
} ;

If your operator is defined before this header is included,
you're going to get some very surprising results when tracing is
turned on.
ok, i got it. Thanks again

Oct 1 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it...
0
by: Ryan M. Keith | last post by:
I am having a problem with the ostream operator in templated classes that I wrote (I'm using the Borland compiler), and I'm certain that the templates are the problem because when I remove the...
4
by: thule | last post by:
Okay, thanks to John (THANK YOU SO MUCH). I switched all my header files over to using the new Standard <iostream> and included the using namespace std; . This seems to have fixed all the errors I...
7
by: Ned Harding | last post by:
In VC7.1 the following code outputs: 00000000 Success! It would seem that the Y::operator<< is hiding the global operator<< when it is outputting an object from another namespace but not when...
2
by: Tatu Portin | last post by:
1: #include <iostream> 2: 3: typedef struct { 4: double r; 5: double i; 6: } complex; .. .. .. 24: ostream & operator<< (ostream &str, const complex &a)
2
by: Toon Knapen | last post by:
Following small test-program (see below) compiles fine with multiple versionS of gcc, icc, xlC, aCC, pathscale but not with the PGI compiler. The PGI 6.1 compiler tells me following: <error>...
19
by: David W | last post by:
float nanometers(long pm) { return pm / 1000.0f; } void f() { if(nanometers(309311L) == nanometers(309311L)) { // do something }
13
by: Aston Martin | last post by:
can anyone point out why compiler is not happy ? it finds ambiguity in the friend function declaration and its implementation. if you eliminate the 'dummy' template parameter altogether, it works....
6
by: syang8 | last post by:
Any one can specify the problem of the following code? The compiling error is on the friend function. If the base class is not inherited from ostream, or I just remove the friend function from the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.