|
Ublas : |
From: Vardan Akopian (vakopian_at_[hidden])
Date: 2007-10-06 01:15:59
On 10/5/07, Nico Galoppo <nico_at_[hidden]> wrote:
> Hi,
>
> I have the snippet of code below that compiles OK with MSVC 8.0, but
> fails with gcc 4.0.1 (OSX). I guess my question is whether my
> templated helper function is actually valid, or violating the C++
> spec.
>
> template <typename V, typename T>
> inline void assign(ublas::vector_expression<V>& bv, const vector3<T>&
> {
> for (unsigned int i = 0; i < 3; ++i)
> bv () (i) = v(i);
> }
>
> int main()
> {
> ublas::vector<float> v(100);
> vec3<float> tuple;
>
> assign(subrange(v, 3,6), tuple);
> }
>
> vec3<T> is a specialized (non-ublas) 3-vector class. gcc gives an
> error at the 'assign' line in main():
>
> error: invalid initialization of non-const reference of type
> 'boost::numeric::ublas::vector_expression<boost::numeric::ublas::vector_range<boost::numeric::ublas::vector<float,
> boost::numeric::ublas::unbounded_array<float, std::allocator<float> >
> > > >&' from a temporary of type
> 'boost::numeric::ublas::vector_range<boost::numeric::ublas::vector<float,
> boost::numeric::ublas::unbounded_array<float, std::allocator<float> >
> > >'
gcc is correct, but the problem is not the templates. The call to
subrange(v, 3,6) creates a temporary, and you're not allowed to pass a
temporary as a non constant reference.
One easy way to fix this is to do:
subrange sr(v, 3,6); // or sr = subrange(v, 3,6), if subrange() is a
function, not a class.
assign(sr, tuple);
-Vardan