Boost logo

Ublas :

Subject: [ublas] Move operators
From: Joaquim Duran Comas (jdurancomas_at_[hidden])
Date: 2015-04-21 19:13:19


Hello all,

I've some questions regarding to move operators:

1 - As Houndie, in request #20 in boost/org/ublas, I would like to include
the rreference operator (&&) before the matrix parameter:

- fixed_matrix &operator = (fixed_matrix m) {
+ fixed_matrix &operator = (fixed_matrix && m) {

This is how I've seen defined the move operator in may places. However, the
comment tells that the parameter is passed by value to enable the move
semantics. I've searched about move operator with parameter by value. I've
found this article
http://cpptruths.blogspot.com.es/2012/03/rvalue-references-in-constructor-when.html
.

The article tells that to activate the move semantics, the parameter should
be passed by value (as you tell), but it is not applied the the move
constructor or move iterator:

class Book {
public:
  Book(std::string title,
       std::vector<std::string> authors, // <-- Parameter by value
       size_t pub_day
       std::string pub_month,
       size_t pub_year)
    : _title (std::move(title)),
      _authors (std::move(authors)), // <-- The parameter is moved to
member variable
      _pub_day (pub_day),
      _pub_month(std::move(pub_month)),
      _pub_year (pub_year)
     {}

  // ....
  // ....
};

2 - AFAIK, with the implementation of move semantics has not deprecated the
copy semantics. As it is defined in the source code, it looks like that
move semantic could not be used with copy semantics. Both semantics should
be used at the same time in a C++11 compiler:

// Copy semantics
matrix m1;
matrix m2(m1);
m2 = m1;

// Move semantics
matrix m1;
matrix m2(std::move(m1));
m3 = std::move(m2);

Please, could you confirm that move operators are defined as expected?

Thanks and Best Regards,
Joaquim Duran