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?