left-hand operand of comma has no effect

  • Follow


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
0
Reply s88 9/22/2010 7:26: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);

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;
}

0
Reply Marc 9/22/2010 7:58:35 PM


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
0
Reply Victor 9/22/2010 8:08:51 PM

s88 ha scritto:

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

Are you sure that will make the code more readable?

I mean... If you say the meaning is a+b, then why don't you simply 
overload operator+()?


-- 
Christian Hackl
hacki@sbox.tugraz.at

Milano 2008/2009 -- L'Italia chiam�, s�!
0
Reply Christian 9/22/2010 11:06:34 PM

On Sep 22, 12:26=A0pm, s88 <dave...@gmail.com> 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?
0
Reply Jim 9/23/2010 6:02:50 PM

4 Replies
1073 Views

(page loaded in 0.039 seconds)

Similiar Articles:













7/21/2012 10:07:52 PM


Reply: