auto return for some recursive functions in C++11
MattCoder via Digitalmars-d
digitalmars-d at puremagic.com
Wed Jan 14 16:13:37 PST 2015
On Wednesday, 14 January 2015 at 22:29:09 UTC, bearophile wrote:
> According to Wikipedia:
> http://en.wikipedia.org/wiki/C%2B%2B14#Function_return_type_deduction
>
> C++14 is able to compile code like this:
>
>
> auto correct(int i) {
> if (i == 1)
> return i;
> else
> return correct(i - 1) + i;
> }
> ...
According to the wiki link:
"If multiple return expressions are used in the function's
implementation, then they must all deduce the same type."
So this will NOT work:
auto Correct(int i) {
if (i == 1){
return (int)5;
}else{
return (float)10;
}
}
Error:
prog.cpp: In function 'auto Correct(int)':
prog.cpp:8:19: error: inconsistent deduction for 'auto': 'int'
and then 'float'
return (float)10;
^
So could you please tell me a good use case to for using "return
type deduction"?
Matheus.
More information about the Digitalmars-d
mailing list