Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does std::reference_wrapper explicitly define a copy assignment operator?

Tags:

c++

A std::reference_wrapper explicitly declares the copy assignment operator. Why? This should prevent move construction/assignment from being implicitly defined so my assumption is that they do not want you to move a std::reference_wrapper, but I am not sure why that would matter.

If you look at the "possible implementation", they just default the operator: https://en.cppreference.com/w/cpp/utility/functional/reference_wrapper/operator%3D

Just looking for some history on why the committee felt the need for this.

like image 655
Rian Quinn Avatar asked Oct 16 '25 13:10

Rian Quinn


2 Answers

They declare it because it has work to do.

Yes, this inhibits an automatically-generated move constructor. No, that doesn't matter. The authors would have had to define their own anyway (for the same reason they need to create a copy constructor), except that there is no use in "moving" something that doesn't own a resource. It would be the same as a copy.

That doesn't mean that a std::reference_wrapper isn't movable, it just means that "moving" one is the same as copying it. And there's no point in implementing that twice!

like image 80
Asteroids With Wings Avatar answered Oct 19 '25 05:10

Asteroids With Wings


This should prevent move construction/assignment from being implicitly defined

std::reference_wrapper is a non-owning reference (a pointer, really), move operation is no different from copy. Don't confuse it with std::shared_ptr.

like image 41
Maxim Egorushkin Avatar answered Oct 19 '25 05:10

Maxim Egorushkin