Topic: function returning const object


Author: jason@cygnus.com (Jason Merrill)
Date: 1995/06/28
Raw View
>>>>> Helmut Jarausch <jarausch@igpm.rwth-aachen.de> writes:

> Please comment on the following example.
> class A
> { int a;
>   public:
>   int get() const { return a; }
>   void put( int x ) { a= x; }
> };

> // my CC (SGI) complains
> // line 9 (the following non comment line)
> //       : warning(1021): type qualifiers are meaningless in this declaration
> //  const A One()

> // and compiles  statement One().put(2);   below without an error message

> const A One()
> { A Temp;
>   Temp.put(1);
>   return Temp;
> }

> int main()
> { int i;
>   i= One().get(); // is and should be OK
>   One().put(2);   // should not compile !
>   return 0;
> }

> --------------------------
> I don't think a 'const' qualifier is meaningless here, and applying
> a non const member function should be an error.

I agree.  In C, rvalues cannot be cv-qualified; in C++, they can.

  3.10  Lvalues and rvalues                                 [basic.lval]

8 Class  rvalues  can  have cv-qualified types; non-class rvalues always
  have cv-unqualified types.  Rvalues always have complete types or  the
  void type; lvalues may have incomplete types.

Jason





Author: shankar@sgi.com (Shankar Unni)
Date: 1995/06/28
Raw View
Jason Merrill (jason@cygnus.com) wrote:

>   3.10  Lvalues and rvalues                                 [basic.lval]

> 8 Class  rvalues  can  have cv-qualified types; non-class rvalues always
>   have cv-unqualified types.  Rvalues always have complete types or  the
>   void type; lvalues may have incomplete types.

This seems to be a clarification since the ARM, and will be fixed in a
future release..
--
Shankar Unni    E-Mail: shankar@sgi.com
Silicon Graphics Inc.   Phone: +1-415-390-2072
URL: http://reality.sgi.com/employees/shankar





Author: jarausch@igpm.rwth-aachen.de (Helmut Jarausch)
Date: 1995/06/27
Raw View
Please comment on the following example.


class A
{ int a;
  public:
  int get() const { return a; }
  void put( int x ) { a= x; }
};

// my CC (SGI) complains
// line 9 (the following non comment line)
//       : warning(1021): type qualifiers are meaningless in this declaration
//  const A One()

// and compiles  statement One().put(2);   below without an error message

const A One()
{ A Temp;
  Temp.put(1);
  return Temp;
}

int main()
{ int i;
  i= One().get(); // is and should be OK
  One().put(2);   // should not compile !
  return 0;
}

--------------------------
I don't think a 'const' qualifier is meaningless here, and applying
a non const member function should be an error.

Thanks for your comments,
Helmut Jarausch.