auto return for some recursive functions in C++11

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Wed Jan 14 14:37:10 PST 2015


On Wed, 14 Jan 2015 22:29:08 +0000
bearophile via Digitalmars-d <digitalmars-d at puremagic.com> 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;
> }
> 
> 
> But not like this:
> 
> auto correct(int i) {
>      if (i != 1)
>          return correct(i - 1) + i;
>      else
>          return i;
> }
> 
> 
> D isn't able to compile both. Is it a good idea to allow the 
> first function in D too?
> 
> Bye,
> bearophile
i'm pretty sure that D *SHOULD* compile the first sample. the spec says
that return type for `auto` function is taken from the first `return`
operator parser met. so for the second `return` the return type is
already known.

i think that this is not just "let it compile", but a bug in compiler.
something should be fixed: either compiler or specs.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20150115/404877d5/attachment-0001.sig>


More information about the Digitalmars-d mailing list