callback vs lambda

前端 未结 4 1946
予麋鹿
予麋鹿 2021-02-05 04:20

Suppose I have the following code that I wish to refactor:

int toFuture()
{
  precalc();
  int calc = 5 * foobar_x() + 3;
  postcalc();
  return calc;
}

int toP         


        
4条回答
  •  隐瞒了意图╮
    2021-02-05 04:28

    I'd say you're refactoring from the wrong side:

    struct CalcGuard {
      CalcGuard() { /* replaces precalc() */ }
      ~CalcGuard() { /* replaces postcalc() */ }
    };
    
    int toFuture()
    {
      return CalcGuard(), calc = 5 * foobar_x() + 3;
    }
    
    int toPast()
    {
      return CalcGuard(), calc = 5 * foobar_y() - 9;
    }
    

提交回复
热议问题