From: Sylwester A. <sa...@ig...> - 2012-07-03 23:42:15
|
Dear Patrik, On 30/04/12 11:34, Sylwester Arabas wrote: > On Apr 30, 2012, at 12:47 AM, Patrik Jonsson wrote: >> I have a hunch -- the array you're generating with B(Range) goes out >> of scope, so the expression is pointing to an array that no longer >> exists. There is a way to make the expression hold a *copy* of the >> Array instead of a reference by using the function safeToReturn(expr). >> Take a look at the safeToReturn.cpp in the testsuite. >> >> Let me know if that helps. > > Yes it does! I'll allow myself to ask yet one more question regarding the C++11 auto return type and the blitz::safeToReturn(): why the code below fails on F_safe() call while it does work with F() call? ------------------------------------------------------------------------ $ cat test.cpp #include <blitz/array.h> using namespace blitz; using namespace std; template <class T1, class T2> auto F(const T1 &a1, const T2 &a2) -> decltype(a1 * a2) { return a1 * a2; } template <class T1, class T2> auto F_safe(const T1 &a1, const T2 &a2) -> decltype(safeToReturn(a1 * a2)) { return safeToReturn(a1 * a2); } int main() { Range i(0, 10); Array<double,1> A(Range(-1, 11)), B(Range(-1, 11)), C(Range( 0, 11)); cerr << "F() ..." << endl; A(i) = F(B(i), C(i)); cerr << "F_safe() ..." << endl; A(i) = F_safe(B(i), C(i)); } $ g++ -DBZ_DEBUG -std=c++11 test.cpp -lblitz $ ./a.out F() ... F_safe() ... /usr/local/include/blitz/bounds.h:71 Two array operands have different lower bounds: in rank 0, the bounds are -1 and 0 a.out: /usr/local/include/blitz/bounds.h:72: static int blitz::bounds::compute_lbound(int, int, int): Assertion `0' failed. Aborted ------------------------------------------------------------------------ Tested with the current version from the repository. Thanks, Sylwester -- http://www.igf.fuw.edu.pl/~slayoo/ |