auto limitation?

Maxim Fomin maxim at maxim-fomin.ru
Tue Sep 11 11:57:07 PDT 2012


On Tuesday, 11 September 2012 at 18:32:47 UTC, Ali Çehreli wrote:
>
> Here is a reduced code:
>

I guess it may be reduced to:

auto foo(bool val)
{
    if (val)
       return 42;
    else
       return 1.5;
}

void main()
{
    assert(foo(true) == 42); // assertion failure
    assert(foo(false) == 1.5);
}


>
> The return type of foo() is double. (It's float in your code 
> but it doesn't matter.)
>
> I think this is a bug. I guess that 'return 42' is still 
> placing an int onto the program stack instead of a float. A 
> workarounds are returning to!float(this._num.ivalue).
>
> But I think this is a compiler bug.
>
> Ali

I think it is UB rather than a bug. The spec says that return 
types must match exactly. AFAIK auto is a feature to infer return 
type, not to magically adjust to multiple incompatible types.



More information about the Digitalmars-d-learn mailing list