const and valarray reference

J

Jim West

The following compiles with g++ 3.3.2, but fails with Intel icc 7.1 with
the error:

asdf.cc(6): error: expression must be an lvalue or a function designator
f1(&arr[0]);

Is this undefined behavior, or have I found a problem with one compiler?

Code sample is:

void f1(const double *);

void f2(const std::valarray<double> &arr) {
f1(&arr[0]);
}
 
V

Victor Bazarov

Jim West said:
The following compiles with g++ 3.3.2, but fails with Intel icc 7.1 with
the error:

asdf.cc(6): error: expression must be an lvalue or a function designator
f1(&arr[0]);

Is this undefined behavior, or have I found a problem with one compiler?

Code sample is:

void f1(const double *);

void f2(const std::valarray<double> &arr) {
f1(&arr[0]);
}

Constant std::valarray's operator[] returns an rvalue. You
cannot take an address of it. What are you trying to accomplish?
Perhaps changing f1 to accept a double const & would help:

void f1(double const&);
void f2(std::valarray<double> const& arr) {
f1(arr[0]);
}

?

Victor
 
J

Jim West

Jim West said:
The following compiles with g++ 3.3.2, but fails with Intel icc 7.1 with
the error:

asdf.cc(6): error: expression must be an lvalue or a function designator
f1(&arr[0]);

Is this undefined behavior, or have I found a problem with one compiler?

Code sample is:

void f1(const double *);

void f2(const std::valarray<double> &arr) {
f1(&arr[0]);
}

Constant std::valarray's operator[] returns an rvalue. You
cannot take an address of it.

Actually I took it from Stroustrup Third edition. My copy is at
work so I can't give the page number now, but he speceifically
states that since the iterator for valarray is a pointer that
&arr[0] is valid.



What are you trying to accomplish?
Perhaps changing f1 to accept a double const & would help:

void f1(double const&);
void f2(std::valarray<double> const& arr) {
f1(arr[0]);
}

?

Victor

I've written several wrappers to overload basic linear algebra subroutine
(BLAS) Fortran functions so I can call each the same way. An actual code
piece (I guess I should have given this before) is


using std::valarray;

extern "C" void daxpy_(const int &n, const double &Alpha, const double *x,
const int &incx, double *y, const int &incy);

inline void axpy(int n, double Alpha, const valarray<double> &x, int incx,
valarray<double> &y, int incy) {
daxpy_(n, Alpha, &x[0], incx, &y[0], incy); }


I also have versions to overload the other three equivalent BLAS routines
(saxpy_, caxpy_, and zaxpy_). Since they are Fortran routines I need to
send a pointer to the first position of the array. daxpy_ does not
change the data in the x array, so I have been telling the compiler that
by adding the const. Did I get it in the wrong place? (Previously I had
been using new/delete to allocate/deallocate the arrays, but a memory leak
that proved difficult to locate convinced me to use the C++ tools as they
were intentended.)

Your modifications to the test code compiled on both compilers. That
may be the fix that I needed. (Although I don't understand why it is
different from what I had...more reading Stroustrup tomorrow!)

Thanks for the input.
 
J

Jim West

The following compiles with g++ 3.3.2, but fails with Intel icc 7.1 with
the error:

asdf.cc(6): error: expression must be an lvalue or a function designator
f1(&arr[0]);

Is this undefined behavior, or have I found a problem with one compiler?

Code sample is:

void f1(const double *);

void f2(const std::valarray<double> &arr) {
f1(&arr[0]);
}

Continuing to follow up my own posts, I the behavior difference
seems to be because the GNU valarray template includes

const _Tp& operator[](size_t) const;

which is missing from the Intel template. I confirmed that this is being
called using the debugger. So, is this a non-standard implementation
of valarray?
 
V

Victor Bazarov

Jim West said:
The following compiles with g++ 3.3.2, but fails with Intel icc 7.1 with
the error:

asdf.cc(6): error: expression must be an lvalue or a function designator
f1(&arr[0]);

Is this undefined behavior, or have I found a problem with one compiler?

Code sample is:

void f1(const double *);

void f2(const std::valarray<double> &arr) {
f1(&arr[0]);
}

Continuing to follow up my own posts, I the behavior difference
seems to be because the GNU valarray template includes

const _Tp& operator[](size_t) const;

which is missing from the Intel template. I confirmed that this is being
called using the debugger. So, is this a non-standard implementation
of valarray?

Well, current standard defines element access for std::valarray as

T operator[](size_t) const;
T& operator[](size_t);

So, yes, the one you quoted would be non-standard.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top