Topic: Address of a temporary object.


Author: "Michael Kochetkov" <mkochetk@trustworks.commm>
Date: 2000/07/27
Raw View
Let us consider the following code:
class A {
public:
  //A* operator &() { return this; } // it is commented intentionally
};
int
main()
{
  A* ap;
  ap = &A();
}

I was unable to find out the words in the Standard that I cannot take the
address of a temporary. The code above cannot be compiled with Comeau C++,
but the other compilers I have tried eat it (some of them emit the warning
about taking address of a temporary). I would appreciate the proper
reference to the Standard that would make it clear.
BTW the explicit operator & makes Comeau C++ happy (the commented line).
Without the explicit operator & the form ap = A().operator &() makes Comeau
C++ to complain with words "class "A" has no member "operator&".  I know
that operator& is not in the list of members that should be generated by a
compiler by default. The second question is why? If my allegations are
correct I whould consider it as a defect (I mean it is a pity it is not in
the list). I realize that there should be a difference between the
fundamental and user defined types.

Thank you in advance,
with regards,
Michael Kochetkov.



---
[ 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: comeau@panix.com (Greg Comeau)
Date: 2000/07/27
Raw View
In article <397f2083@news.telekom.ru>,
Michael Kochetkov <mkochetk@trustworks.commm> wrote:
>Let us consider the following code:
>class A {
>public:
>  //A* operator &() { return this; } // it is commented intentionally
>};
>int
>main()
>{
>  A* ap;
>  ap = &A();
>}
>
>I was unable to find out the words in the Standard that I cannot take the
>address of a temporary. The code above cannot be compiled with Comeau C++,
>but the other compilers I have tried eat it (some of them emit the warning
>about taking address of a temporary). I would appreciate the proper
>reference to the Standard that would make it clear.
>BTW the explicit operator & makes Comeau C++ happy (the commented line).
>Without the explicit operator & the form ap = A().operator &() makes Comeau
>C++ to complain with words "class "A" has no member "operator&".  I know
>that operator& is not in the list of members that should be generated by a
>compiler by default. The second question is why? If my allegations are
>correct I whould consider it as a defect (I mean it is a pity it is not in
>the list). I realize that there should be a difference between the
>fundamental and user defined types.

A() is not a modifiable lvalue, but the builtin & takes an lvalue.

Whether or not an op& is synthesized or not won't change
this IMO, if one argues that it would still get rejected.

- greg
--
Comeau Computing / Comeau C/C++ 4.2.42 (4.2.44 expected soon)
TRY Comeau C++ ONLINE at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.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: jpotter@falcon.lhup.edu (John Potter)
Date: 2000/07/28
Raw View
On Thu, 27 Jul 2000 03:34:08 CST, "Michael Kochetkov"
<mkochetk@trustworks.commm> wrote:

> Let us consider the following code:
> class A {
> public:
>   //A* operator &() { return this; } // it is commented intentionally
> };
> int
> main()
> {
>   A* ap;
>   ap = &A();
> }
>
> I was unable to find out the words in the Standard that I cannot take the
> address of a temporary.

5.3.1/2 The operand of & must be an lvalue.

3.10    Temporaries are rvalues (See also lifetime)

> BTW the explicit operator & makes Comeau C++ happy (the commented line).

3.10/10 A member function may modify an rvalue.

> Without the explicit operator & the form ap = A().operator &() makes Comeau
> C++ to complain with words "class "A" has no member "operator&".  I know
> that operator& is not in the list of members that should be generated by a
> compiler by default. The second question is why? If my allegations are
> correct I whould consider it as a defect (I mean it is a pity it is not in
> the list). I realize that there should be a difference between the
> fundamental and user defined types.

The global operator& has known behavior.  A member modifies it.  What
should the default member do?

A const& ar = A();
ap = const_cast<A*>(&ar);

That is as nasty as it should be.  At least the temporary still exists.
In the above, you would have taken the address of something which was
going to self destruct at the end of the statement taking its address.
Your member function provides this wonderful dangling pointer.

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: "Michael Kochetkov" <mkochetk@trustworks.commm>
Date: 2000/07/28
Raw View
"Greg Comeau" <comeau@panix.com> wrote in message
news:8lpcjb$eu9$1@panix6.panix.com...
> In article <397f2083@news.telekom.ru>,
> Michael Kochetkov <mkochetk@trustworks.commm> wrote:
> >Let us consider the following code:
> >class A {
> >public:
> >  //A* operator &() { return this; } // it is commented intentionally
> >};
> >int
> >main()
> >{
> >  A* ap;
> >  ap = &A();
> >}
> >
> >I was unable to find out the words in the Standard that I cannot take the
> >address of a temporary. The code above cannot be compiled with Comeau
C++,
> >but the other compilers I have tried eat it (some of them emit the
warning
> >about taking address of a temporary). I would appreciate the proper
> >reference to the Standard that would make it clear.
> >BTW the explicit operator & makes Comeau C++ happy (the commented line).
> >Without the explicit operator & the form ap = A().operator &() makes
Comeau
> >C++ to complain with words "class "A" has no member "operator&".  I know
> >that operator& is not in the list of members that should be generated by
a
> >compiler by default. The second question is why? If my allegations are
> >correct I whould consider it as a defect (I mean it is a pity it is not
in
> >the list). I realize that there should be a difference between the
> >fundamental and user defined types.
>
> A() is not a modifiable lvalue,
I agree.

> but the builtin & takes an lvalue.
I do feel the same but I was unable to find it out in the Standard.

> Whether or not an op& is synthesized or not won't change
> this IMO, if one argues that it would still get rejected.
Sorry, I am not sure I understand you. Do you mean that operaror & should
never be called for rvalue?

Thank you in advance,
with regards,
Michael Kochetkov.



---
[ 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: "Michael Kochetkov" <mkochetk@trustworks.commm>
Date: 2000/07/28
Raw View
> > I was unable to find out the words in the Standard that I cannot take
the
> > address of a temporary.
>
> 5.3.1/2 The operand of & must be an lvalue.
Thank you. I have missed it.

>
> 3.10    Temporaries are rvalues (See also lifetime)
>
> > BTW the explicit operator & makes Comeau C++ happy (the commented line).
>
> 3.10/10 A member function may modify an rvalue.
Right. That is why I have provided the explicit operator &.

[...]

> In the above, you would have taken the address of something which was
> going to self destruct at the end of the statement taking its address.
> Your member function provides this wonderful dangling pointer.
Right again.And this is still feasible with the explicit operator &. But
user-defined operator & may return whatever it wants (and it is not
necessary the address of itself)... So, it sounds wise indeed to forbid the
implicit operator & generation. Thank you for explanation and ideas. The
implicit result result of you reply for me was that I have come to a
conclusion that:
class A {};
int
main()
{
 A* ap;
 A aobj;
 ap = aobj.operator&(); // calls member operator & which is missing
(ill-formed)
 ap = &aobj; // calls global operator & (well-formed)
}
Well, I can take addresses of objects in C++ now.

With regards,
Michael Kochetkov.



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