Why does reference_wrapper implement operator() in particular?

270 Views Asked by At

std::reference_wrapper is a standard library that wraps a reference in a copyable, assignable object.

My view is that it has a weird design and seems like a hack to trick classes that store objects by value to believe that they are copying a value instead of storing a reference.

It also seems like a "tag" to pass by reference.

Without getting into any controversy about the semantics of the class:

What I find particularly puzzling is that it implements operator()(...) (forwarding it to the internal reference).

Does anybody know why it implements such a member specifically? I mean, it could implement many other operators, like ==, or [], so why () only?

With that criterion, it should implement any common operator +, ==, <, [], swap (not to mention, any arbitrary member like `.size(), if that were possible in the language).

A possibility that I imagine is that std::reference_wrapper is many times used to store functions, but it seems just odd, and in that case reference_function would have been a better name.

0

There are 0 best solutions below