Move operators

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Move operators

Joaquim Duran Comas
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


_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Move operators

Nasos Iliopoulos
Joaquim,
the move semantics in uBlas were implemented before the C++11 standard was adopted. We will need update the implementation to reflect the C++11 changes with respect to move constructors. I would say that gcc's vector implementation of the C++11 constructors design is a good reference implementation.

Best,
Nasos


On 04/21/2015 07:13 PM, Joaquim Duran Comas wrote:
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



_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]


_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Move operators

Joaquim Duran Comas
Ok, I see. I was thinking that it was intended to be compatible with C++11.

Joaquim Duran


2015-04-23 20:07 GMT+02:00 Nasos Iliopoulos <[hidden email]>:
Joaquim,
the move semantics in uBlas were implemented before the C++11 standard was adopted. We will need update the implementation to reflect the C++11 changes with respect to move constructors. I would say that gcc's vector implementation of the C++11 constructors design is a good reference implementation.

Best,
Nasos



On 04/21/2015 07:13 PM, Joaquim Duran Comas wrote:
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



_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]


_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]


_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Move operators

Joaquim Duran Comas
I've taken a look to g++ implementation of vector class:

The copy operator 'operator=(const vector &v)' is defined for any version of c++ language, however the move operator 'operator=(vector &&v)' is only defined if c++11 standard is activated.

So, for ublas we could use the c++11 based move operator if c++11 standard is activated or use previous move operator if not. So, the code could be something like:

class matrix {

...

matrix& operator=(const matrix& m); // always defined

#ifdef BOOST_UBLAS_CPP_GE_2011
matrix& operator=(matrix&& m);
#else
matrix& operator=(matrix& m);
#endif

....

}

Joaquim Duran


2015-04-23 23:55 GMT+02:00 Joaquim Duran Comas <[hidden email]>:
Ok, I see. I was thinking that it was intended to be compatible with C++11.

Joaquim Duran


2015-04-23 20:07 GMT+02:00 Nasos Iliopoulos <[hidden email]>:
Joaquim,
the move semantics in uBlas were implemented before the C++11 standard was adopted. We will need update the implementation to reflect the C++11 changes with respect to move constructors. I would say that gcc's vector implementation of the C++11 constructors design is a good reference implementation.

Best,
Nasos



On 04/21/2015 07:13 PM, Joaquim Duran Comas wrote:
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



_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]


_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]



_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]