473,671 Members | 2,215 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::ostrea m&))« 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 2420

"Thomas Lenz" <le****@gmx.dew rote in message
news:48******** **************@ newsspool3.arco r-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::ostrea m&))« 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.dew rote:
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::ostrea m&))« 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_DEBUGG ING
#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 objektorientier ter Datenverarbeitu ng
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.dew rote:
>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<cla ss 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::ostrea m&))« 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_DEBUGG ING
#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
8259
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 at all possible?
0
2067
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 templating completely everything works perfectly. Here is the error message: Error: Unresolved external 'operator <<(std::basic_ostream<char, std::char_traits<char> >&, const BinomialTree<int>&)' referenced from C:\DOCUMENTS AND...
4
6382
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 was getting because of mixing the old and new headers. The problem I am running into now is not an error but 10 warnings when compiling the same class header implimentation file that I was working with when I was having the problems with the map...
7
1143
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 it is outputting an object in the global namespace. I can't help but think one of these 2 behaviors is a bug in VC7.1, but which one?
2
1288
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
2147
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> "test.cpp", line 12: error: more than one operator "<<" matches these operands: function "std::operator<<(std::basic_ostream<char, std::char_traits<char>> &, const std::complex<double> &)"
19
1754
by: David W | last post by:
float nanometers(long pm) { return pm / 1000.0f; } void f() { if(nanometers(309311L) == nanometers(309311L)) { // do something }
13
369
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. what effect 'dummy' is having ? it is important to have the function as friend, since so i can make SomeClass look-n-feel as builtin. #include<iostream> using namespace std; template<typename T, int dummy>
6
4718
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 base class, the code will compile. ------------------------------------------------------------------------- #include <iostream> template<class Tclass base; template<class Tstd::ostream& operator<< (std::ostream&, base<T>&);
0
8472
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8390
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8819
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8596
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8667
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4221
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1801
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.