Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
* Mysterious DEC OSF C++ compiler warning
@ 1995-12-18  5:00 Marc Roussel
  1995-12-18  5:00 ` Morgan Deters
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Marc Roussel @ 1995-12-18  5:00 UTC (permalink / raw)


Here's a simple program for which the DEC OSF C++ compiler generates a (to me)
mysterious warning:

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

// foo.c
#include <iostream.h>
#include <math.h>
#include "foo.h"


main()
{
    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)".

Programs which generate this warning nevertheless appear to execute correctly.
Is the DEC compiler getting excited about nothing?  Is it warning me about
nonportable usage?  Bad style?  What's the scoop?  If I'm doing something
wrong, is there a reasonably easy workaround?  Note that the line which reads
"x=junk(2)" can be replaced with "x=2" and still generates a similar warning.
Finally, I should mention that code like this generates no warnings with the
somewhat old Borland compiler on my home machine.

Thanks in advance for any help you can offer.
-- 
					Marc R. Roussel
					(roussel@henri.chem.uleth.ca)
					Department of Chemistry
					University of Lethbridge




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Mysterious DEC OSF C++ compiler warning
  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
  1995-12-20  5:00 ` Tim Lucia
  2 siblings, 0 replies; 4+ messages in thread
From: Morgan Deters @ 1995-12-18  5:00 UTC (permalink / raw)


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

> Here's a simple program for which the DEC OSF C++ compiler generates a (to me)
> mysterious warning:
> 
> // foo.h:
> class junk
>         { double a;
>           public:
>           junk(void) {};
>           junk(double x) { a = x;};
>           junk & operator = (junk & x) { a = x.a; return(*this);};
>         };
> 
> // foo.c
> #include <iostream.h>
> #include <math.h>
> #include "foo.h"
> 
> 
> main()
> {
>     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)".

junk::operator=(junk&) takes a non-const reference as a parameter.  You
are supplying a temporary, junk(2), to the operator=() method.

If operator=() is not to modify its argument (which it really shouldn't
do, if you model your overloaded operators after built-in operator
concepts), declare operator=() as:
   junk& operator=(const junk& x) { a = x.a; return *this; }

This will clear the problem.

The compiler doesn't like it because the temporary junk(2) could be altered,
which is ridiculous because you no longer have access to it.
____________________________________________________________________
Morgan Deters                                    mdeters@cs.bgsu.edu
Bowling Green State University                   Bowling Green, Ohio



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Mysterious DEC OSF C++ compiler warning
  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
  1995-12-20  5:00 ` Tim Lucia
  2 siblings, 0 replies; 4+ messages in thread
From: Andrew Koenig @ 1995-12-19  5:00 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Mysterious DEC OSF C++ compiler warning
  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
@ 1995-12-20  5:00 ` Tim Lucia
  2 siblings, 0 replies; 4+ messages in thread
From: Tim Lucia @ 1995-12-20  5:00 UTC (permalink / raw)



#include <iostream.h>

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

main()
{
    junk x;

    x = junk(2);
}

// ARM, 12.8 (pp. 295-296)
//
// If all bases and members of a class X have copy constructors accepting
// const arguments, the generated copy constructor for X will take a
// single argument of type const X&, as follows (double has the const ones)
//
// X::X(const X&)
//
// Otherwise it will take a single arugment of type X&:
//
// X::X(X&)
//
// Similarly, for assignment operators:
//
// If all bases...
//
// X& X::operator=(const X&)
//
// or
//
// X& X::operator=(X&) // which is what you have
//
// Since you declared a non-const assignment operator, then the constructor
// junk(2) does not match any declared constructor (default X& X::operator=(const X&))
// so you get the warning on conversion to using the non-const one.
//
// use this:   junk & operator = (const junk & x) { a = x.a; return(*this);};


Regards,
Tim

-- 
Timothy J. Lucia                         Email:    mailto:lucia@zko.dec.com
Digital Equipment Corporation            Phone:           603.881.2615
110 Spitbrook Road ZKO2-3/K06            Fax:             603.881.0120
Nashua, NH  USA  03062-2698              I'd rather be cycling.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1995-12-20  5:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
1995-12-20  5:00 ` Tim Lucia

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