18

I am trying to understand how std::ref works.

#include <functional>
#include <iostream>

template <class C>
void func(C c){
    c += 1;
}

int main(){
    int x{3};
    std::cout << x << std::endl;
    func(x);
    std::cout << x << std::endl;
    func(std::ref(x));
    std::cout << x << std::endl;
}

Output : 3 3 4

In the code above, I think the template parameter C for the third function call is instantiated as std::reference_wrapper<int>. While reading the reference, I noticed there is no += operator in std::reference_wrapper<int>. Then, how is c += 1; valid?

2 Answers 2

19

how is c += 1; valid?

Because reference_wrapper<int> is implicitly convertible to int& via its conversion operator; and implicit conversions are considered for operands if there is no suitable overload for the operand type itself.

Sign up to request clarification or add additional context in comments.

2 Comments

@Sungmin: Indeed, c itself can't change type; but += is applied to the result of c.operator int&() which will be a reference to target of the reference_wrapper.
Note this magic only works for operators. For example, if x were of class type and had a method x.foo(), and you tried to wrap a reference to x in a reference_wrapper rx, you could not just say rx.foo(). You would have to say rx.get().foo(), which is about as ugly as just using a pointer px->foo. So unfortunately std::reference_wrapper is not a magical do-all wrapper for references (which is what a lot of people hope to use it for, e.g. for copying objects with reference members).
12

std::reference_wrapper<T> has a conversion operator to T&. This means that std::reference_wrapper can be implicitly converted to int& in your example.

2 Comments

The two answers were uploaded almost the same time. But Mike Seymour's answer is a little bit faster. Apart from that, I cannot determine which one to pick. Sorry for that. Thanks anyway.
@Sungmin You are welcome. I think Seymour's answer is better anyway: he explains about the operators overloads and operands, which I failed to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.