How is tr1::reference_wrapper useful?

前端 未结 2 1029
闹比i
闹比i 2020-12-03 07:38

recently I\'ve been reading through Scott Meyers\'s excellent Effective C++ book. In one of the last tips he covered some of the features from TR1 - I knew many of them via

2条回答
  •  暖寄归人
    2020-12-03 07:45

    It's like boost::ref, as far as I know. Basically, a reference which can be copied. Very useful when binding to functions where you need to pass parameters by reference.

    For example (using boost syntax):

    void Increment( int& iValue )
    {
        iValue++;
    }
    
    int iVariable = 0;
    boost::function< void () > fIncrementMyVariable = boost::bind( &Increment, boost::ref( iVariable ));
    
    fIncrementMyVariable();
    

    This Dr. Dobbs article has some info.

    Hope this is right, and helpful. :)

提交回复
热议问题