www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - implicit enum conversions

reply "John C" <johnch_atms hotmail.com> writes:
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)'. 
Dec 20 2005
parent reply Oskar Linde <oskar.lindeREM OVEgmail.com> writes:
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
Dec 20 2005
next sibling parent "John C" <johnch_atms hotmail.com> writes:
"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.
Dec 20 2005
prev sibling parent BCS <BCS_member pathlink.com> writes:
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), ..."
Dec 20 2005