Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
From: ark@research.att.com (Andrew Koenig)
Subject: Re: Mysterious DEC OSF C++ compiler warning
Date: 1995/12/19	[thread overview]
Message-ID: <DJu7zy.Lx1@research.att.com> (raw)
In-Reply-To: 4b4rog$m80@holly.cc.uleth.ca

In article <4b4rog$m80@holly.cc.uleth.ca> Marc Roussel <roussel@henri.chem.uleth.ca> writes:

> // foo.h:
> class junk
>         { double a;
>           public:
>           junk(void) {};
>           junk(double x) { a = x;};
>           junk & operator = (junk & x) { a = x.a; return(*this);};
>         };

// ...
>     junk x;

>     x = junk(2);

> The warning is

> foo.c:10: warning: In this statement, the initialization of a non-const
> reference requires a temporary for "junk(2)".

When you defined the copy constructor for junk, you said it requires
an lvalue as its argument because you said

	junk & operator = (junk & x) { /* ... */ }

instead of

	junk & operator = (const junk & x) { /* ... */ }

A parameter that is a reference to const will accept an rvalue, but not
a parameter that is a plain reference.  After all, if you did not intend to
modify the parameter, why did you not make it const?

So when you call

	x = junk(2);

you have said you intend to modify 2, which you can't do.  By rights, the
compiler should reject your program with an error message.   However, many
compilers generate a temporary and warn about it because that's what
cfront used to do.  The reason for cfront's behavior, in turn, is that
it dates back to when that was actually the intended compiler behavior.
-- 
				--Andrew Koenig
				  ark@research.att.com



  parent reply	other threads:[~1995-12-19  5:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-12-18  5:00 Mysterious DEC OSF C++ compiler warning Marc Roussel
1995-12-18  5:00 ` Morgan Deters
1995-12-19  5:00 ` Andrew Koenig [this message]
1995-12-20  5:00 ` Tim Lucia
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox