Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > left-hand operand of comma has no effect

Reply
Thread Tools

left-hand operand of comma has no effect

 
 
s88
Guest
Posts: n/a
 
      09-22-2010
Hi all,

I like to implement a new syntax in C++ for my work. It supposed to be
(a,b) , which represents a+b, for example.
My implementation as following...

inline myClass operator () (myClass &left, myClass &right);

the usage of this operator is
cout << (a,b) << endl;

However, g++ reports
"warning: left-hand operand of comma has no effect"

Does it mean the 'a' has no effect? How should I do?

thanks,

Dave
 
Reply With Quote
 
 
 
 
Marc
Guest
Posts: n/a
 
      09-22-2010
s88 wrote:

> I like to implement a new syntax in C++ for my work. It supposed to be
> (a,b) , which represents a+b, for example.
> My implementation as following...
>
> inline myClass operator () (myClass &left, myClass &right);


That's not how operator() works, it is only for member functions, to be
used as myobject(left,right).

> the usage of this operator is
> cout << (a,b) << endl;
>
> However, g++ reports
> "warning: left-hand operand of comma has no effect"
>
> Does it mean the 'a' has no effect? How should I do?


You can't redefine the meaning of parentheses, but you can redefine that
of commas.

struct A { int i; };
int operator,(A a,A b){return a.i+b.i;}
int main(){
A a,b;
a.i=2;
b.i=3;
std::cout << (a,b) << std::endl;
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-22-2010
On 9/22/2010 3:26 PM, s88 wrote:
> I like to implement a new syntax in C++ for my work. It supposed to be
> (a,b) , which represents a+b, for example.
> My implementation as following...
>
> inline myClass operator () (myClass&left, myClass&right);
>
> the usage of this operator is
> cout<< (a,b)<< endl;
>
> However, g++ reports
> "warning: left-hand operand of comma has no effect"
>
> Does it mean the 'a' has no effect? How should I do?


The function call operator is only definable as a member of a class, not
as a stand-alone function. The compiler understands your syntax as

cout << (operator,(a,b)) << endl;

and, of course, 'a' has no effect and 'b' is the value of the expression
in the parentheses.

What you might want is to define the operator comma in your class, like so:

class myClass {
...
public:
myClass operator,(myClass const& right) const {
myClass const& left = *this;
... // do what you did earlier with 'left' and 'right'
}
};

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      09-23-2010
On Sep 22, 12:26*pm, s88 <(E-Mail Removed)> wrote:
> Hi all,
>
> I like to implement a new syntax in C++ for my work. It supposed to be
> (a,b) , which represents a+b, for example.
> My implementation as following...
>
> inline myClass operator () (myClass &left, myClass &right);
>
> the usage of this operator is
> cout << (a,b) << endl;
>
> However, g++ reports
> "warning: left-hand operand of comma has no effect"
>
> Does it mean the 'a' has no effect? How should I do?


If I do
std::cout << (1, 2, 3);
the output is 3. The right most value is output. In this particular
case, 1 and 2 do absolutely nothing since they have no side effects.

However, if I do
std::cout << ( foo(), bar(), 3 );
then foo() and bar() would have effect, even though they are not
output they may have side effects.

Now, since ( x, y ) works this way, are you positive you want to
change C++'s behavior for your own classes to act differently? What
is wrong with just using a+b?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does left shift operation invoke Undefined Behaviour when theleft side operand has negative value? Prasoon Saurav C++ 1 09-24-2010 06:07 PM
csv.reader has trouble with comma inside quotes inside brackets Bret Python 3 06-09-2009 09:27 PM
error C2296: '.*' : illegal, left operand has type 'cStepCurveEvaluator *const ' BlueTrin C++ 6 04-05-2006 10:45 AM
'AddressOf' operand must be the name of a method; no parentheses a =?Utf-8?B?UGF0cmljay5PLklnZQ==?= ASP .Net 15 10-18-2004 11:49 PM
Unsupported error,& Right operand of "Divide" operator must be a power of 2.. Kim JM VHDL 1 04-02-2004 06:58 PM



Advertisments