Topic: Defect Report: Return Type of auto_ptr::operator=
Author: "Joseph Gottman" <joegottman@worldnet.att.net>
Date: 2000/06/30 Raw View
[ forwarded to C++ committee. -sdc ]
According to section 20.4.5, the function auto_ptr::operator=() returns a
reference to an auto_ptr. The reason that operator=() usually returns a
reference is to facilitate code like
int x,y,z;
x = y = z = 1;
However, given analogous code for auto_ptrs,
auto_ptr<int> x, y, z;
z.reset(new int(1));
x = y = z;
the result would be that z and y would both be set to Null, instead of all
the auto_ptrs being set to the same value. This makes such cascading
assignments useless and counterintuitive for auto_ptrs.
Proposed Solution:
Change auto_ptr::operator=() to return void instead of an auto_ptr
reference.
[ 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: hinnant@anti-spam_metrowerks.com (Howard Hinnant)
Date: 2000/07/02 Raw View
In article <ViS65.22794$pu6.542946@bgtnsc06-news.ops.worldnet.att.net>,
"Joseph Gottman" <joegottman@worldnet.att.net> wrote:
| [ forwarded to C++ committee. -sdc ]
|
| According to section 20.4.5, the function auto_ptr::operator=() returns a
| reference to an auto_ptr. The reason that operator=() usually returns a
| reference is to facilitate code like
| int x,y,z;
| x = y = z = 1;
|
| However, given analogous code for auto_ptrs,
|
| auto_ptr<int> x, y, z;
| z.reset(new int(1));
| x = y = z;
|
| the result would be that z and y would both be set to Null, instead of all
| the auto_ptrs being set to the same value. This makes such cascading
| assignments useless and counterintuitive for auto_ptrs.
|
| Proposed Solution:
|
| Change auto_ptr::operator=() to return void instead of an auto_ptr
| reference.
That would break this currently legal code:
auto_ptr<int> z(new int(1));
auto_ptr<int> y;
int x;
x = *(y = z);
-Howard
---
[ 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: "Joseph Gottman" <joegottman@worldnet.att.net>
Date: 2000/07/03 Raw View
Howard Hinnant <hinnant@anti-spam_metrowerks.com> wrote in message
news:hinnant-2906002226260001@ith3-492.twcny.rr.com...
> In article <ViS65.22794$pu6.542946@bgtnsc06-news.ops.worldnet.att.net>,
> "Joseph Gottman" <joegottman@worldnet.att.net> wrote:
>
> | [ forwarded to C++ committee. -sdc ]
> |
> | According to section 20.4.5, the function auto_ptr::operator=() returns
a
> | reference to an auto_ptr. The reason that operator=() usually returns a
> | reference is to facilitate code like
> | int x,y,z;
> | x = y = z = 1;
> |
> | However, given analogous code for auto_ptrs,
> |
> | auto_ptr<int> x, y, z;
> | z.reset(new int(1));
> | x = y = z;
> |
> | the result would be that z and y would both be set to Null, instead of
all
> | the auto_ptrs being set to the same value. This makes such cascading
> | assignments useless and counterintuitive for auto_ptrs.
> |
> | Proposed Solution:
> |
> | Change auto_ptr::operator=() to return void instead of an auto_ptr
> | reference.
>
> That would break this currently legal code:
>
> auto_ptr<int> z(new int(1));
> auto_ptr<int> y;
> int x;
> x = *(y = z);
>
You're right. What if we made operator= return a const reference?
Operator*() is a const member function, so your code would work, and
cascading assignments would still be illegal.
---
[ 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: hinnant@anti-spam_metrowerks.com (Howard Hinnant)
Date: 2000/07/03 Raw View
In article <pjv75.1964$oh5.125407@bgtnsc04-news.ops.worldnet.att.net>,
"Joseph Gottman" <joegottman@worldnet.att.net> wrote:
| You're right. What if we made operator= return a const reference?
| Operator*() is a const member function, so your code would work, and
| cascading assignments would still be illegal.
Perhaps someone prefers cascading assignments as a notational short cut:
auto_ptr<T> a1(...);
auto_ptr<T> a2(...);
auto_ptr<T> a3(...);
auto_ptr<T> a4(...);
...
// Set a1 to a4 and release everything else
a1 = a2 = a3 = a4; // or a1 = a4;
// a2.release();
// a3.release();
...
We could spend days (or years!) arguing the merits of either style. But
the fact remains that the style on the left has been standard C++ for two
years. What motivation does the standards committee have to break the
hypothetical programmer's code on the left side? You've got to have a
really good reason to change the standard in such a way that it is not
backward compatible with existing code.
In article <ViS65.22794$pu6.542946@bgtnsc06-news.ops.worldnet.att.net>,
"Joseph Gottman" <joegottman@worldnet.att.net> wrote:
| This makes such cascading assignments useless and counterintuitive for
| auto_ptrs.
Counterintuitive? Probably. Useless? I'm not convinced. I don't see
sufficient motivation to risk breaking existing code.
There are issues where I _do_ see sufficient motivation for potentially
breaking existing code. But I don't think this is one of them.
Please note that I have stated only my opinion. I have not stated a
fact. The C++ committee (and/or other people who read this thread) may
well feel otherwise. Please continue to scrutinize the standard and bring
to our attention any issues you have (and thanks for bringing this one
up!).
-Howard
---
[ 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: Paul Jensen <pdjensen@agileimage.com>
Date: 2000/07/04 Raw View
Howard Hinnant wrote:
>
> In article <ViS65.22794$pu6.542946@bgtnsc06-news.ops.worldnet.att.net>,
> "Joseph Gottman" <joegottman@worldnet.att.net> wrote:
>
> | [ forwarded to C++ committee. -sdc ]
> |
> | According to section 20.4.5, the function auto_ptr::operator=() returns a
> | reference to an auto_ptr. The reason that operator=() usually returns a
> | reference is to facilitate code like
> | int x,y,z;
> | x = y = z = 1;
> |
> | However, given analogous code for auto_ptrs,
> |
> | auto_ptr<int> x, y, z;
> | z.reset(new int(1));
> | x = y = z;
> |
> | the result would be that z and y would both be set to Null, instead of all
> | the auto_ptrs being set to the same value. This makes such cascading
> | assignments useless and counterintuitive for auto_ptrs.
> |
> | Proposed Solution:
> |
> | Change auto_ptr::operator=() to return void instead of an auto_ptr
> | reference.
>
> That would break this currently legal code:
>
> auto_ptr<int> z(new int(1));
> auto_ptr<int> y;
> int x;
> x = *(y = z);
>
Just to elaborate since the original example by Joseph Gottman seemed to
misunderstand this point, C++ uses the lvalue of the left hand side as the
result of the assignment, not the rvalue of the right hand side as C does. Hence
y and z would be null, and x would hold the pointer.
Another way of stating what Howard Hinnant wrote is:
int y, z;
assert( &(y = z) == &y );
--
Paul Jensen Agile Image Movers http://agileimage.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 ]