Understanding Safety of Function Pointers vs. Addresses of Functions

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 14 10:24:40 PDT 2015


On Tuesday, 14 July 2015 at 14:02:46 UTC, jmh530 wrote:
> Thanks for posting that. I figured out the issue. Before you 
> had recommended that I use
> alias cos = std.math.cos;
> I had kept that text in. When I removed it, everything worked 
> just fine. I'm still not sure I grasp the subtleties of alias 
> when used with templates.

Oh that's another goodie. So you had a situation like this:
----
import std.math;

alias cos = std.math.cos;

mixin template t()
{
     void cos(real[] arr) {/* ... */}
}

mixin t!();

void main()
{
     cos([1, 2, 3]);
}
----

This fails with "Error: None of the overloads of 'cos' are 
callable using argument types (int[])".

The problem is that template mixins cannot add to existing 
overload sets. The spec says: "If the name of a declaration in a 
mixin is the same as a declaration in the surrounding scope, the 
surrounding declaration overrides the mixin one" [1]. That means, 
the `cos` from `alias cos = std.math.cos;` completely overrides 
the one from `mixin t!();`. I guess this is a measure against 
function hijacking again.

I'm not sure if it's supposed to work like it does when the alias 
is removed, two implicitly imported/generated symbols forming an 
overload set. But I can't immediately see a problem with it 
either.

[1] http://dlang.org/template-mixin.html - see "Mixin Scope"


More information about the Digitalmars-d-learn mailing list