Is this std::ref behaviour logical?

前端 未结 3 1192
独厮守ぢ
独厮守ぢ 2021-02-04 23:29

Consider this code:

#include 
#include 

int xx = 7;

template
void f1(T arg)
{
    arg += xx;
}

template

        
3条回答
  •  攒了一身酷
    2021-02-04 23:43

    arg = xx;

    Local arg now refers to (read as binds with) xx. (And no more refers to j)

    arg += xx;

    Implicit operator T& () is applied to match the argument of operator += and hence addition is performed on referred object i.e. j.

    So the observed behaviour is correct.

提交回复
热议问题