www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - misoverloading

reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--bcaec54ee8a4041d1d04d01a026b
Content-Type: text/plain; charset=UTF-8

There's a curiously misleading behavior when overloading on the same
underlying types:

struct Test
{
void* ptr;
uint num;
}

alias const(Test) A;

void foo(A)
{
import std.stdio;
writeln("mutable");
}

void foo(const(A))
{
import std.stdio;
writeln("const");
}

unittest
{
foo(A());
}

DMD outputs the following error:
C:\Users\g.gyolchanyan\Desktop\test.d(67): Error: function test.foo called
with argument types:
((const(Test)))
matches both:
C:\Users\g.gyolchanyan\Desktop\test.d(53): test.foo(const(Test) _param_0)
and:
C:\Users\g.gyolchanyan\Desktop\test.d(59): test.foo(const(Test) _param_0)

The error should be about redefinition of foo(), since A and const(A) are
the exact same type.
Is this a bug or am I mistaken on the expected behavior?

-- 
Bye,
Gor Gyolchanyan.

--bcaec54ee8a4041d1d04d01a026b
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div>There&#39;s a curiously misleading behavior when overloading on the sa=
me underlying types:</div><div><br></div><div>struct Test</div><div>{</div>=
<div><span class=3D"" style=3D"white-space:pre">	</span>void* ptr;</div><di=
v>
<span class=3D"" style=3D"white-space:pre">	</span>uint num;</div><div>}</d=
iv><div><br></div><div>alias const(Test) A;</div><div><br></div><div>void f=
oo(A)</div><div>{</div><div><span class=3D"" style=3D"white-space:pre">	</s=
pan>import std.stdio;</div>
<div><span class=3D"" style=3D"white-space:pre">	</span>writeln(&quot;mutab=
le&quot;);</div><div>}</div><div><br></div><div>void foo(const(A))</div><di=
v>{</div><div><span class=3D"" style=3D"white-space:pre">	</span>import std=
.stdio;</div>
<div><span class=3D"" style=3D"white-space:pre">	</span>writeln(&quot;const=
&quot;);</div><div>}</div><div><br></div><div>unittest</div><div>{</div><di=
v><span class=3D"" style=3D"white-space:pre">	</span>foo(A());</div><div>}<=
/div>
<div><br></div><div>DMD outputs the following error:</div><div><div>C:\User=
s\g.gyolchanyan\Desktop\test.d(67): Error: function test.foo called with ar=
gument types:</div><div><span class=3D"" style=3D"white-space:pre">	</span>=
((const(Test)))</div>
<div>matches both:</div><div><span class=3D"" style=3D"white-space:pre">	</=
span>C:\Users\g.gyolchanyan\Desktop\test.d(53): test.foo(const(Test) _param=
_0)</div><div>and:</div><div><span class=3D"" style=3D"white-space:pre">	</=
span>C:\Users\g.gyolchanyan\Desktop\test.d(59): test.foo(const(Test) _param=
_0)</div>
</div><div><br></div><div>The error should be about redefinition of foo(), =
since A and const(A) are the exact same type.</div><div>Is this a bug or am=
 I mistaken on the expected behavior?</div><div><br></div>-- <br>Bye,<br>
Gor Gyolchanyan.<br>

--bcaec54ee8a4041d1d04d01a026b--
Dec 05 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 12/05/2012 01:20 PM, Gor Gyolchanyan wrote:
 There's a curiously misleading behavior when overloading on the same
 underlying types:

 struct Test
 {
     void* ptr;
     uint num;
 }

 alias const(Test) A;

 void foo(A)
 {
     import std.stdio;
     writeln("mutable");
 }

 void foo(const(A))
 {
     import std.stdio;
     writeln("const");
 }

 unittest
 {
     foo(A());
 }

 DMD outputs the following error:
 C:\Users\g.gyolchanyan\Desktop\test.d(67): Error: function test.foo
 called with argument types:
 ((const(Test)))
 matches both:
 C:\Users\g.gyolchanyan\Desktop\test.d(53): test.foo(const(Test) _param_0)
 and:
 C:\Users\g.gyolchanyan\Desktop\test.d(59): test.foo(const(Test) _param_0)

 The error should be about redefinition of foo(), since A and const(A)
 are the exact same type.
 Is this a bug or am I mistaken on the expected behavior?

 --
 Bye,
 Gor Gyolchanyan.

DMD does not check overloads at all. If you do not call the function, the multiple definition error is caught in the linker. I think there are already reports for this.
Dec 05 2012