TITLE: unary plus operator (Newsgroups: comp.lang.c++,comp.lang.c++.moderated, 17 Nov 98) WAHLE: "Peter J. Wahle" > In some code I'm working on I've found a typing error that is not flagged as > an error when compiled (VC++ 5.0), and it seems to run correctly. The > typing error is the + sign on the first line of code below. What exactly is > this + sign doing? Was it just ignored by the compiler or is it valid C++? > > if( fabs(dC) <= accuracy ) {+ > accuracy = fabs(dC); > return ZC; > } CLAMAGE: Steve Clamage wrote in message <72ftq1$k9a$1@engnews2.Eng.Sun.COM>... > >Unary plus is allowed as an operator, so the code is treated as > > > > if( fabs(dC) <= accuracy ) { > > + accuracy = fabs(dC); > > return ZC; > > } > > > >But the unary plus turns the left side of the assignment into an > >rvalue, so the assignment is technically disallowed. Evidently your > >compiler is incorrectly treating the unary plus as having no effect. > >(It has no effect, except for turning an lvalue into an rvalue > >because the combination is an expression, not an object.) SCHREIB: Dirk Schreib > > Is an operator+() NOT allowed to return a non-const reference? BONNARD: Valentin Bonnard It is allowed to return anything (that can be returned), including for example a pointer to a function: Foo f; (+f) (); Now, if accuracy is of type float, one obviously can't overload operator+ on it. BTW, even if you return a rvalue of user defined type, that doesn't prevents it to be used on the left side of the assignement.