This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: invalid cast of an rvalue expression


Hi Oren,
On Wed, May 18, 2011 at 07:36:03AM -0700, Foster Boondoggle wrote:
> 
> Hi Adam99 - Did you ever get a reply to this? I have the exact same problem
> (indeed, with SqMatrix and Matrix, though I had though I wrote the class
> many years ago!) I can't seem to get it to compile in any form with the
> latest gcc.

Yes, that question was answered by Ian, see e.g.
http://old.nabble.com/Re%3A-invalid-cast-of-an-rvalue-expression-p23106343.html
The point is: It is illegal C++ :-) 
Old compilers (like g++ 4.1.2) accepted such constructs, but the new
gcc-versions detect it as failure

In addition: what this line does, is the following:
 - take "this", a pointer to a  "const SqMatrix"
 - convert it to a Pointer to a "Matrix" (already problematic: you
   remove the const-ness here...)
 - take the element and multiply it with "sm"
 - convert the pointer to a constant reference to a SqMatrix
 - create a copy of this reference and return it.

that sounds a bit complex to me. 

In order to propose a solution, we would need more information (e.g. how
are the classes defined? is SqMatrix derived from Matrix? are there any
Conversion-Operators / Copy-Constructors defined which can be used for
this conversions? 
The easiest solution for this code segment would be to add a new
copy-constructor to SqMatrix:
class SqMatrix {
  public:
    explicit SqMatrix(const Matrix &m) : ...{
     assert(m.Rows() == m.Columns());
     ....
   }
};
which verifies that m is really square, and then creates a SqMatrix from
m (I added "explicit" in order to block implicit type-conversions Matrix
-> SqMatrix -- this constructor has to be called directly).

With that, you could rewrite your code-segment as

      SqMatrix  operator * (const SqMatrix &sm) const {
          return SqMatrix(*(const Matrix *)this * sm); }

which would be save (the type-conversion is cleanly done) and portable.
(however, as I don't know the full code, may be this solution poses
other problems?)

Axel (I'm not adam99 ;-))
> 
> adam99 wrote:
> > 
> > 
> > 
> > I am getting a following error on g++ 4.1.2
> > 
> > 
> > SqMatrix.h: In member function 'SqMatrix SqMatrix::operator*(const
> > SqMatrix&) const':
> > SqMatrix.h:91: error: invalid cast of an rvalue expression of type
> > 'Matrix' to type 'const SqMatrix&'
> > 
> > 
> > for the following code segment
> > 
> >      SqMatrix  operator * (const SqMatrix &sm) const {
> >          return (const SqMatrix &) (*(Matrix *)this * sm); }
> > 
> > 
> > this error did not seem to appear on g++ 3.4.6. Do you know what could be
> > the problem?
> > 
> > Thanks
> > 
> > 
> > 
> > 
> 
> -- 
> View this message in context: http://old.nabble.com/invalid-cast-of-an-rvalue-expression-tp23105605p31647448.html
> Sent from the gcc - Help mailing list archive at Nabble.com.
> 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]