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: left-hand operand of comma has no effect - comp.lang.c++ ...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 fo... Major differences between MIPS and ARM - comp.archleft-hand operand of comma has no effect - comp.lang.c++ ... Major differences between MIPS and ARM - comp.arch Stoopid barrel shifter for one operand in ARM ... very ... Does PAUSE have any Side Effect ?? - comp.lang.fortranBut, you've posted versions before that weren't very big _BUT_ left ... left-hand operand of comma has no effect - comp.lang.c++ ... Does PAUSE have any Side Effect ... How to overload subsref correctly? - comp.soft-sys.matlab ...left-hand operand of comma has no effect - comp.lang.c++ ... How to overload subsref correctly? - comp.soft-sys.matlab ... left-hand operand of comma has no effect ... Very fast delimited record parsing with boost - comp.lang.c++ ...If I didn't find a '\n', I copied what was left of the ... In effect, the class istringstream gives you an elegant ... At this point it can really just be a comma-delimited ... Finding ERRORs in SASLOG - comp.soft-sys.sas... release" > > "export cancelled" > > "expression has no ... no data sets qualify" > > "no data" > > "no effect ... what string does an edit text have when left empty ... Sampling: What Nyquist Didn't Say, and What to Do About It - comp ...But that had ugly side effects for sampled harmonics ... you read the left half of the line with our left hand ... g., one of my "letters of reference" has NO letterhead ... Neatest way to get the end pointer? - comp.lang.cSince "C has no array values" is one way of ... from a pointer, the result has the type of the pointer operand. ... the code *assuming* that there's no UB. In effect, in ... improve strlen - comp.lang.asm.x86It has no stack frame and conforms to the ... block-copied into the slot in the left sidehand argument object. No ... see function call so any possible side effect ... problem in interface - comp.lang.fortran> plz help Off-hand, I'd guess that something ... issue; in fact, I'm quite sure it has come up with you before. No, you ... > I overlooked the fact that the number of commas ... c++ - left-hand operand of comma has no effect? - Stack OverflowI'm having some trouble with this warning message, it is implemented within a template container class. int k = 0, l = 0; for ( k =(index+1), l=0; k < sizeC, l ... left-hand operand of comma has no effect - comp.lang.c++ ...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 fo... 7/21/2012 10:07:52 PM
|