Re: const pointer to const valarray element
Jeremy Sanders wrote:
Hi - I want to get a const pointer to a valarray element:
#include <valarray>
void func(const std::valarray<double> *x)
{
const double* ptr = &((*x)[0]);
}
int main()
{
std::valarray<double> foo(10);
func(&foo);
return 0;
}
This code compiles fine under gcc 4.4.3, but fails under MS Visual C++
Express Edition 2008 with "error C2102: '&' requires l-value".
Stroustup says valarray has a "T operator[](size_t) const" and a "T&
operator[](size_t)".
Is gcc or MSVC correct?
Think about it. Your 'valarray' (to which 'x' points) is a _const_
object, isn't it? So, how would a non-const member function be called
for it? It can't. So, the former op[] is called. It returns a
temporary which is *not* an lvalue. MSVC is correct, gcc isn't.
Is there a portable way to get a const pointer to an element of a const
valarray?
I am not sure what you're trying to accomplish with it. The language
forces you to jump through hoops for a reason: perhaps you shouldn't do
it, you know. What would a const pointer do for you that a regular
*value* can't? The only reason to prefer a pointer over a value is if
you are going to change the value. But with a const valarray you're not
supposed to, right? So, why do you need a pointer?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask