Thread overview
implicit enum conversions
Dec 20, 2005
John C
Dec 20, 2005
Oskar Linde
Dec 20, 2005
John C
Dec 20, 2005
BCS
December 20, 2005
If the default underlying type of an enum member is int, why do I get the error message below when the compiler tries to find a match for a function overload?

enum Option { FIRST, SECOND }

test(bit value) {}
test(int value) {}
test(uint value) {}
test(float value) {}
test(double value) {}

int main() {
    test(Option.FIRST);
    return 0;
}

"function test called with argument types:
    (Option)
matches both:
    test(bit)
and:
    test(double)"

Surely the best match would be 'test(int)'.


December 20, 2005
John C wrote:
> If the default underlying type of an enum member is int, why do I get the error message below when the compiler tries to find a match for a function overload?
> 
> enum Option { FIRST, SECOND }
> 
> test(bit value) {}
> test(int value) {}
> test(uint value) {}
> test(float value) {}
> test(double value) {}
> 
> int main() {
>     test(Option.FIRST);
>     return 0;
> }
> 
> "function test called with argument types:
>     (Option)
> matches both:
>     test(bit)
> and:
>     test(double)"
> 
> Surely the best match would be 'test(int)'. 

The DMD error message just displays two possible matches. There could be more. See the front end source, func.c FuncDeclaration::overloadResolve()

/Oskar
December 20, 2005
"Oskar Linde" <oskar.lindeREM@OVEgmail.com> wrote in message news:do8ue1$1g5$1@digitaldaemon.com...
> John C wrote:
>> If the default underlying type of an enum member is int, why do I get the error message below when the compiler tries to find a match for a function overload?
>>
>> enum Option { FIRST, SECOND }
>>
>> test(bit value) {}
>> test(int value) {}
>> test(uint value) {}
>> test(float value) {}
>> test(double value) {}
>>
>> int main() {
>>     test(Option.FIRST);
>>     return 0;
>> }
>>
>> "function test called with argument types:
>>     (Option)
>> matches both:
>>     test(bit)
>> and:
>>     test(double)"
>>
>> Surely the best match would be 'test(int)'.
>
> The DMD error message just displays two possible matches. There could be more. See the front end source, func.c FuncDeclaration::overloadResolve()
>
> /Oskar

So it seems that if there is more than one possible match, it's an error, as with numeric conversions.

I've a suggestion: if the base type is specified on an enum (eg, enum Option : int), then that enum can only be implicitly converted to its base type, otherwise a cast is required and an error is reported.


December 20, 2005
Oskar Linde wrote:
> John C wrote:
> 
[...]
>>
>> "function test called with argument types:
>>     (Option)
>> matches both:
>>     test(bit)
>> and:
>>     test(double)"
>>
>> Surely the best match would be 'test(int)'. 
> 
> 
> The DMD error message just displays two possible matches. There could be more. See the front end source, func.c FuncDeclaration::overloadResolve()
> 
> /Oskar

maybe the error should be:

 "function test called with argument types:
     (Option)
 has two or more matches:
     test(bit),
     test(double),
     ..."