Topic: ANSI Syntax error ???


Author: grzegorz <grzegorz@jps.net>
Date: 2000/05/19
Raw View
> But still ,is it ANSI syntax error ???
>
> cause if not there will be not deferent between ++++x;     and     a+b=5;
>
> Meyer's "More effective C++" deals with that by define const when ever
> returning by value and the object must not be change.
>
> what you think about that ???
>
> Thanks
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

I don't know why do you have problem with expression a+b=5,
because (a+b)=5 works for me :
#include <iostream>
class A
{
int i;
public:
A operator +(A& x) { return A(i+x.i); }
A operator=(A& x) {  return A(x.i); }
A(int x) : i(x) {}
void out() { cout << " value " << i; }
};
int main()
{
A x1(5),x2(6),x3(7);
((x1+x2)=x3).out();
};

--
  grzegorz


======================================= MODERATOR'S COMMENT:
 [Please try not to quote moderation banners in submissions to comp.std.c++.]

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Shanir" <shanir@netvision.net.il>
Date: 2000/03/16
Raw View
Hi .

I am wondering about what ANSI say about temporary type that returns from
functions or member functions.

do you think that :

Obj x(5);

++(++x);

is a ANSI syntax error when class Obj is defined like that :

class Obj{
    int a;
public:
    Obj(int aa):a(aa){}
    Obj operator++( ) { a++;
                                    return *this;
                       }

};


notice that this class is return Obj by value , so a temporary Obj is
created.
g++ "thinks" it's ok (of course, x.a is 6 and not 7 and the temp is being
change then destroy) ,

But still ,is it ANSI syntax error ???

cause if not there will be not deferent between ++++x;     and     a+b=5;

Meyer's "More effective C++" deals with that by define const when ever
returning by value and the object must not be change.

what you think about that ???


Thanks




---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Robert W. Hand" <rwhand@operamail.com>
Date: 2000/03/16
Raw View
Dear Shanir,

I have often seen a programmer post code that does odd things.  Usually, the
explanation is that the code does not really conform to the standard.  I
believe that this is another example.  My copy of the standard uses a
reference for the return type of the prefix operator().  (See 13.5.7).

The real problem is that your code for the class does not mirror the
behavior of the prefix operator ++()  for built in types.  Adding a
reference for the return type restores the behavior that I would have
expected for the prefix operator++().

--
Best wishes,

Bob

> Obj x(5);
>
> ++(++x);
>
> is a ANSI syntax error when class Obj is defined like that :
>
> class Obj{
>     int a;
> public:
>     Obj(int aa):a(aa){}
>     Obj operator++( ) { a++;
>                                     return *this;
>                        }
>
> };



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 2000/03/16
Raw View

Shanir wrote:

>
> But still ,is it ANSI syntax error ???
>
Why should it be?

> cause if not there will be not deferent between ++++x;     and     a+b=5;

You've lost me.  The issue is not that you can't modify a temporary, it's
that it's not an lvalue.  You can invoke member functions on any class,
even when it is not an lvalue.  You're overloaded operator++ doesn't require
an lvalue, so it works.  The built in assignement operator does (pretty much
wheret he term lvalue/rvalue comes from) and hence won't work in your
a+b = 5;

>
> Meyer's "More effective C++" deals with that by define const when ever
> returning by value and the object must not be change.

Which is correct.  Being an rvalue doesn't mean it's const.  If you want
const, say const.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Darin Adler <darin@bentspoon.com>
Date: 2000/03/17
Raw View
In article <38d02635_4@news1.prserv.net>, "Robert W. Hand"
<rwhand@operamail.com> wrote:

> The real problem is that your code for the class does not mirror the
> behavior of the prefix operator ++()  for built in types.  Adding a
> reference for the return type restores the behavior that I would have
> expected for the prefix operator++().

There's nothing in the standard that requires overloaded operators to
mimic the behavior of the operator for built-in types. It may be a good
practice and one to be encouraged, but it's not required.

    -- Darin

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]