Topic: Is this legal code?


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

Eric Nagler wrote:
>
> On Thu, 24 Feb 2000 03:52:57 CST, Ronny <ronald_f@myremarq.com> wrote:
> >IMO, the code is wrong. The initializer s() is an rvalue, which
> >you can't use to initialize a non-const reference.
>
> Really? Are you then saying that:
>
> A obj = A();
>
> is also invalid? How about:
>
> int x = int();
>

No,  he's not saying that.  He said you can't initalize a non-const
REFERENCE to a rvalue.  Neither of your examples involve initialization
of references, they initialize objects using copy initialization.

---
[ 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: o.brandt@tu-bs.de
Date: 2000/02/25
Raw View
In article <38B41BA1.E45665B@physik.tu-muenchen.de>,
  Christopher Eltschka <celtschk@physik.tu-muenchen.de> wrote:
> Oliver Brandt wrote:
> >
> > Hey everybody!
> >
> > I'm trying to compile a PCCTS-cpp-grammar with the latest
WIN32-GNU-compiler
> > (MINGW32 V2.95.2).
> > Compilation failed because there is a code analog to the following
snippet:
> >
> > class A
> > {
> >         protected:
> >                 struct s
> >                 {
> >                         bool x;
> >                         bool y;
> >                         int  z;
> >                 };
> >                 void foo(s& s_ref = s());
> > };
> >
> > "g++ -c test.cpp" results in:
> > test.cpp:10: invalid type `A::s' for default argument to `A::s &'
> >
> > But the same code compiles fine with Borland 5.4 (aka Borland
Builder 4.0)
> > and Visual C++ 5.0.
> >
> > Question: Which compiler is right?
>
> g++ is right. If I understand correctly, your C++ code is
> machine generated. Therefore the best solution (other than
> fixing the generating tool, of course ;-)) is to give the
> flag -fpermissive to g++. This should change the error into
> a warning.
>

Thanks to everyone who answered my question.
Just a comment to this special answer: No, it's not generated code.
It's hand-written "action"-code for a CPP-Parser developed with
PCCTS by John Lilley (and others ;-).

Regards

Oliver



Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ 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: Ronny <ronald_f@myremarq.com>
Date: 2000/02/24
Raw View
In article <38B282E0.239635C4@sican.de>, Oliver Brandt
<oliver.brandt@sican.de> wrote:
>class A
>{
> protected:
>  struct s
>  {
>   bool x;
>   bool y;
>   int  z;
>  };
>  void foo(s& s_ref = s());
>};
>
>"g++ -c test.cpp" results in:
>test.cpp:10: invalid type `A::s' for default argument to `A::s
&'

IMO, the code is wrong. The initializer s() is an rvalue, which
you can't use to initialize a non-const reference.


Ronald Fischer <ronald_f@myremarq.com>
http://profiles.yahoo.com/ronny_fischer/
http://fusshuhn.ourfamily.com/cppincomp.html

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!

---
[ 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/02/24
Raw View

Oliver Brandt wrote:
>
> "g++ -c test.cpp" results in:
> test.cpp:10: invalid type `A::s' for default argument to `A::s &'

This is correct.  You can't initialize a non-const reference from a
temporary.

---
[ 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: Rob Stewart <donotspamme@giage.com>
Date: 2000/02/24
Raw View
Ronny wrote:
>
> In article <38B282E0.239635C4@sican.de>, Oliver Brandt
> <oliver.brandt@sican.de> wrote:
> >class A
> >{
> >       protected:
> >               struct s
> >               {
> >                       bool x;
> >                       bool y;
> >                       int  z;
> >               };
> >               void foo(s& s_ref = s());
> >};
> >
> >"g++ -c test.cpp" results in:
> >test.cpp:10: invalid type `A::s' for default argument to `A::s
> &'
>
> IMO, the code is wrong. The initializer s() is an rvalue, which
> you can't use to initialize a non-const reference.

I agree, and so does the Comeau online compiler:

Comeau C/C++ 4.2.42 (Nov 22 1999 11:52:18) for ONLINE_EVALUATION
Copyright 1988-1999 Comeau Computing.  All rights reserved.

"986.c", line 10: error: initial value of reference to non-const
must be an
          lvalue
                  void foo(s& s_ref = s());
                                      ^

--
Robert Stewart     |  rob-at-giage-dot-com
Software Engineer  |  using std::disclaimer;
Giage, Ltd.        |  http://www.giage.com

---
[ 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: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 2000/02/24
Raw View
Oliver Brandt wrote:
>
> Hey everybody!
>
> I'm trying to compile a PCCTS-cpp-grammar with the latest WIN32-GNU-compiler
> (MINGW32 V2.95.2).
> Compilation failed because there is a code analog to the following snippet:
>
> class A
> {
>         protected:
>                 struct s
>                 {
>                         bool x;
>                         bool y;
>                         int  z;
>                 };
>                 void foo(s& s_ref = s());
> };
>
> "g++ -c test.cpp" results in:
> test.cpp:10: invalid type `A::s' for default argument to `A::s &'
>
> But the same code compiles fine with Borland 5.4 (aka Borland Builder 4.0)
> and Visual C++ 5.0.
>
> Question: Which compiler is right?

g++ is right. If I understand correctly, your C++ code is
machine generated. Therefore the best solution (other than
fixing the generating tool, of course ;-)) is to give the
flag -fpermissive to g++. This should change the error into
a warning.

---
[ 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: Eric Nagler <epn@eric-nagler.com>
Date: 2000/02/24
Raw View
On Thu, 24 Feb 2000 03:52:57 CST, Ronny <ronald_f@myremarq.com> wrote:
>IMO, the code is wrong. The initializer s() is an rvalue, which
>you can't use to initialize a non-const reference.

Really? Are you then saying that:

A obj = A();

is also invalid? How about:

int x = int();

---
[ 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: Michiel Salters <salters@lucent.com>
Date: 2000/02/25
Raw View
Eric Nagler wrote:
>
> On Thu, 24 Feb 2000 03:52:57 CST, Ronny <ronald_f@myremarq.com> wrote:
> >IMO, the code is wrong. The initializer s() is an rvalue, which
> >you can't use to initialize a non-const reference.

> Really? Are you then saying that:

> A obj = A();

> is also invalid? How about:

Legal. A& obj = A(); would be illegal, though. At the semicolon, the
destructor of the temporary is called. If it were legal, obj would
not refer to an A - which would violate an invariant of C++.

> int x = int();

Same thing. int x = int(); is legal : a copy is made. int &x = int();
has the same problem.

Michiel Salters

---
[ 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: jpotter@falcon.lhup.edu (John Potter)
Date: 2000/02/25
Raw View
On Thu, 24 Feb 2000 21:54:04 CST, Eric Nagler <epn@eric-nagler.com>
wrote:

: On Thu, 24 Feb 2000 03:52:57 CST, Ronny <ronald_f@myremarq.com> wrote:
: >IMO, the code is wrong. The initializer s() is an rvalue, which
: >you can't use to initialize a non-const reference.

---------------------------------^^^^^^^^^^^^^^^^^^^

: Really?

Yes.  You seem to have missed a reference somewhere.

: Are you then saying that:
:
: A obj = A();
:
: is also invalid?

No but

  A& ar = A();

is invalid, while

  A const& acr = A();

is valid.

John

---
[ 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: Paul Black <paul.black@oxsemi.com>
Date: 2000/02/25
Raw View
Eric Nagler <epn@eric-nagler.com> wrote:
>
> On Thu, 24 Feb 2000 03:52:57 CST, Ronny <ronald_f@myremarq.com> wrote:
> >IMO, the code is wrong. The initializer s() is an rvalue, which
> >you can't use to initialize a non-const reference.
>
> Really? Are you then saying that:
>
> A obj = A();
>
> is also invalid? How about:
>
> int x = int();

Errm, obj and x are not references.

Paul

---
[ 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: Oliver Brandt <oliver.brandt@sican.de>
Date: 2000/02/23
Raw View
Hey everybody!

I'm trying to compile a PCCTS-cpp-grammar with the latest WIN32-GNU-compiler
(MINGW32 V2.95.2).
Compilation failed because there is a code analog to the following snippet:

class A
{
 protected:
  struct s
  {
   bool x;
   bool y;
   int  z;
  };
  void foo(s& s_ref = s());
};

"g++ -c test.cpp" results in:
test.cpp:10: invalid type `A::s' for default argument to `A::s &'

But the same code compiles fine with Borland 5.4 (aka Borland Builder 4.0)
and Visual C++ 5.0.

Question: Which compiler is right?

Regards

Oliver Brandt

---
[ 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: Michiel Salters <salters@lucent.com>
Date: 2000/02/24
Raw View
Oliver Brandt wrote:
>
> Hey everybody!

> I'm trying to compile a PCCTS-cpp-grammar with the latest WIN32-GNU-compiler
> (MINGW32 V2.95.2).
> Compilation failed because there is a code analog to the following snippet:

> class A {
>         protected:
>                 struct s {
>                         bool x;
>                         bool y;
>                         int  z;
>                 };
>                 void foo(s& s_ref = s());
> };

> "g++ -c test.cpp" results in:
> test.cpp:10: invalid type `A::s' for default argument to `A::s &'

> But the same code compiles fine with Borland 5.4 (aka Borland Builder 4.0)
> and Visual C++ 5.0.

> Question: Which compiler is right?

I'm not sure if this really should be an error, but this code is
certainly dubious. a.foo() would modify s_ref (else its type
would have been 's const&', but this would be a temporary not
visible to the user of a.foo().

So I'd argue the correct type should be 'A::s const', not 'A::s'.

Of course, 'void A::foo() { s _s; foo(_s) };' is a quick solution.

Michiel Salters

---
[ 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              ]