Boost logo

Ublas :

Subject: [ublas] solution to assignment to nested proxies in development?
From: oswin krause (oswin.krause_at_[hidden])
Date: 2014-06-16 05:55:00


Hi everyone,

is there currently a solution implemented in uBLAS for the following
problem?

blas::matrix<double> mymat(2,20);
noalias(subrange(row(mymat,0),0,10) = ...;

this will fail as row(mymat,0) returns a temporary object which leads to
subrange being called with something constant, which leads to a constant
object fed to noalias -> compilation fails, this is not possible.

I think that rvalue references in C++11 might be a solution to this as
one can just overload subrange with an rvalue version (i hope, i am not
using C++11 unfortunately). Also there is a slight problem when some
lunatic calls proxies on a temporary container and assigns something to
this. In that case some expressions would happily be compiled even
though it should be a compile error. My current C++03 compatible
solution uses an additional object.

template<class E>
struct temporary_proxy: public E{
     temporary_proxy(E& e):E(e){}
};

//and for all proxies i have got three versions

//non-const version
template<class E>
temporary_proxy<my_proxy<E> > my_proxy_creator(vector_expression<E> & e){
  return my_proxy<E>(e);
}

//const version
template<class E>
temporary_proxy<my_proxy<E> > my_proxy_creator(vector_expression<E>
const& e){
  return my_proxy<E const>(e);
}

//temporary proxy version
template<class E>
temporary_proxy<my_proxy<E> > my_proxy_creator(temporary_proxy<E> e){
  return my_proxy_creator(static_cast<E&>(e));
}

using this scheme, the code above happily compiles.

I stumbled into this problem a huge number of times, therefore i came up
with this solution. Is there maybe a better way?

Regards,
Oswin