c++ - How do reference_wrapper and std::ref work? -
i trying 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 code above, think template parameter c
third function call instantiated std::reference_wrapper<int>
. while reading the reference, noticed there no +=
operator in std::reference_wrapper<int>
. then, how c += 1;
valid?
how
c += 1;
valid?
because reference_wrapper<int>
implicitly convertible int&
via conversion operator; , implicit conversions considered operands if there no suitable overload operand type itself.
Comments
Post a Comment