www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - null matches typed pointers before void pointers

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
I'm not sure if this is a bug or not:

struct Foo { }

void test(void* test) { assert(0); }
void test(Foo* test)  { }

void main()
{
    test(null);
}

The call matches the second overload: "void test(Foo* test)". But
shouldn't this be an ambiguous call?
May 24 2012
parent reply Justin Whear <justin economicmodeling.com> writes:
On Thu, 24 May 2012 14:50:38 +0200, Andrej Mitrovic wrote:

 I'm not sure if this is a bug or not:
 
 struct Foo { }
 
 void test(void* test) { assert(0); }
 void test(Foo* test)  { }
 
 void main()
 {
     test(null);
 }
 
 The call matches the second overload: "void test(Foo* test)". But
 shouldn't this be an ambiguous call?
Perhaps it should be ambiguous due to the special nature of null, but I think I understand why dmd isn't treating it as such. Firstly, null got promoted to its own type (is(typeof(null) != void*)) a few releases ago so it won't exactly match on the void* overload. Secondly, the Foo* overload is more specific (any Foo* can implicitly convert to a void*) so the overload picker goes with it. Justin
May 24 2012
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Justin Whear:

 Perhaps it should be ambiguous due to the special nature of 
 null,
Maybe a compile-time error is better here. Bye, bearophile
May 24 2012
parent "bearophile" <bearophileHUGS lycos.com> writes:
 Maybe a compile-time error is better here.
http://d.puremagic.com/issues/show_bug.cgi?id=8146
May 24 2012