Return type deduction in recursive function

前端 未结 2 1312
甜味超标
甜味超标 2021-01-05 06:01

Following code compiles :

auto foo(int i) {
  if( i == 1 )
    return i;
  else 
    return foo(i-1)+i ; 
}

While following doesn\'t,

2条回答
  •  渐次进展
    2021-01-05 06:41

    A recursive function can have an auto return type only if it has a non-recursive return statement before the recursive call. See Return type deduction for normal functions.

提交回复
热议问题