----- Original Message ----- From: Ivan Vecerina To: boost-users@yahoogroups.com Sent: Thursday, February 13, 2003 3:41 PM Subject: [Boost-Users] Re: UBLAS: copy on write for matrix & vector?
wrote in message news:b2g7nj+6gee@eGroups.com... | A question on use of the matrix and vector classes: | | Should I: ... | matrix<double> foo(const matrix<double> a) ... | void foo(matrix<double>& r, const matrix<double> a) ... | In other words: is it expensive to construct the temporary in case | one? As I understand, the class would need an underlying 'copy on | write' to be cheap, similar as strings do. By default, the copy constructor of boost::numeric::ublas::matrix appears to copy its representation in a newly allocated block. As far as I can tell, this behavior could be customized by using a custom storage type (the third template parameter).
Yup. One could play ugly games with class array_adaptor<> for example, but I'd tend to advise against it.
However, many optimizing compilers are able to eliminate unnecessary copying of the return value of a function. (This is called RVO or NRVO for Return Value Optimization and Named..., depending on how the function is written). For example, MSVC suppresses the unnecessary copy, even in Debug mode with no optimizations enabled.
So I think you should prefer the interface which is more natural to use: matrix<double> foo(const matrix<double> a)
Generally agreed, although ublas' bench1 shows, what can happen (especially for small dimensions) if temporaries are involved. Thanks, Joerg