 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Bronek Kozicki Guest
|
Posted: Sun Feb 22, 2004 6:00 pm Post subject: const valarray::operator[] returning rvalue ? |
|
|
According to standard (clause 26.3.2.3) element access operator for
const std::valarray have signature:
T operator[](size_t) const;
It seems strange - I'm unable to do simple thing like:
void f(const std::valarray<int>& c)
{
const int * p = &c[0];
}
however, because returned rvalue is non-const I can modify it, which
seems really unnecessary relaxation of valarray const-ness (why would
anoyone want to modify this temporary value ?). Apparently GCC have
non-confirming implementation of this operator:
const _Tp& operator[](size_t) const;
which seems better (consistent with containers and allowing certain
const-lvalue operations, like taking pointer to element). Can anybody
explain why standard mandates returning rvalue ?
Thanks
B.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Daniel Krügler (nee Spang Guest
|
Posted: Mon Feb 23, 2004 5:25 pm Post subject: Re: const valarray::operator[] returning rvalue ? |
|
|
Good Morning Bronek Kozicki,
Bronek Kozicki schrieb:
| Quote: | According to standard (clause 26.3.2.3) element access operator for
const std::valarray have signature:
T operator[](size_t) const;
It seems strange - I'm unable to do simple thing like:
void f(const std::valarray<int>& c)
{
const int * p = &c[0];
}
however, because returned rvalue is non-const I can modify it, which
seems really unnecessary relaxation of valarray const-ness (why would
anoyone want to modify this temporary value ?). Apparently GCC have
non-confirming implementation of this operator:
const _Tp& operator[](size_t) const;
which seems better (consistent with containers and allowing certain
const-lvalue operations, like taking pointer to element). Can anybody
explain why standard mandates returning rvalue ?
|
Your intuition is right. This problem is already resolved according to
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-active.html#389
in the sense that the corresponding operator signature should be changed to
const T& operator[](size_t const);
Greetings from Bremen,
Daniel Krügler
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Seungbeom Kim Guest
|
Posted: Mon Feb 23, 2004 9:53 pm Post subject: Re: const valarray::operator[] returning rvalue ? |
|
|
Bronek Kozicki wrote:
| Quote: |
According to standard (clause 26.3.2.3) element access operator for
const std::valarray have signature:
T operator[](size_t) const;
It seems strange - I'm unable to do simple thing like:
void f(const std::valarray<int>& c)
{
const int * p = &c[0];
}
however, because returned rvalue is non-const I can modify it, which
seems really unnecessary relaxation of valarray const-ness (why would
anoyone want to modify this temporary value ?). Apparently GCC have
non-confirming implementation of this operator:
const _Tp& operator[](size_t) const;
which seems better (consistent with containers and allowing certain
const-lvalue operations, like taking pointer to element). Can anybody
explain why standard mandates returning rvalue ?
|
It seems to have been discussed before and, strangely, one of two
relevant items is marked NAD(not a defect) and the other Ready:
http://std.dkuug.dk/jtc1/sc22/wg21/docs/lwg-closed.html#77
http://std.dkuug.dk/jtc1/sc22/wg21/docs/lwg-active.html#389
The latter one is later than the former one, so I presume the
committee have changed their mind and now consider it as a defect.
--
Seungbeom Kim
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Bronek Kozicki Guest
|
Posted: Mon Feb 23, 2004 9:54 pm Post subject: Re: const valarray::operator[] returning rvalue ? |
|
|
On Mon, 23 Feb 2004 11:25:58 CST, Daniel Krügler (nee Spangenberg)
wrote:
Thanks for reference. I think there is typo in the issue documentation.
There is:
| Quote: | citation begin
In the class synopsis in 26.3.2 [lib.template.valarray], and in 26.3.2.3 |
[lib.valarray.access] just above paragraph 1, change
T operator[](size_t const);
to
const T& operator[](size_t const);
there should be :
| Quote: | updated version begin
In the class synopsis in 26.3.2 [lib.template.valarray], and in 26.3.2.3 |
[lib.valarray.access] just above paragraph 1, change
T operator[](size_t) const;
to
const T& operator[](size_t) const;
| Quote: | updated version end
|
Kind regards
B.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Gabriel Dos Reis Guest
|
Posted: Wed Feb 25, 2004 5:47 pm Post subject: Re: const valarray::operator[] returning rvalue ? |
|
|
Bronek Kozicki <brok (AT) rubikon (DOT) pl> writes:
| Quote: | According to standard (clause 26.3.2.3) element access operator for
const std::valarray have signature:
T operator[](size_t) const;
It seems strange - I'm unable to do simple thing like:
void f(const std::valarray<int>& c)
{
const int * p = &c[0];
}
however, because returned rvalue is non-const I can modify it, which
seems really unnecessary relaxation of valarray const-ness (why would
anoyone want to modify this temporary value ?). Apparently GCC have
non-confirming implementation of this operator:
const _Tp& operator[](size_t) const;
|
Previous GNU implementations contained the "T" returning
operator[]() const version. That was changed to reflect
(1) to the proposed resolution; and
(2) to match real-world use.
| Quote: | which seems better (consistent with containers and allowing certain
const-lvalue operations, like taking pointer to element). Can anybody
explain why standard mandates returning rvalue ?
|
I would say, it is was oversight in trying to use the existing C++
type system to encode alias-freeness.
--
Gabriel Dos Reis
[email]gdr (AT) cs (DOT) tamu.edu[/email]
Texas A&M University -- Computer Science Department
301, Bright Building -- College Station, TX 77843-3112
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Gabriel Dos Reis Guest
|
Posted: Wed Feb 25, 2004 5:47 pm Post subject: Re: const valarray::operator[] returning rvalue ? |
|
|
Seungbeom Kim <musiphil (AT) bawi (DOT) org> writes:
[...]
Basically, yes.
It think that for "archeological purposes", it was decided to create
new issue items instead of trying to rewrite history.
--
Gabriel Dos Reis
[email]gdr (AT) cs (DOT) tamu.edu[/email]
Texas A&M University -- Computer Science Department
301, Bright Building -- College Station, TX 77843-3112
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|