template sequence parameters treats member functions differently?

Steven Schveighoffer schveiguy at yahoo.com
Mon Jun 25 15:06:42 UTC 2018


On 6/24/18 5:19 PM, aliak wrote:
> Hi, I'm having some issues with template sequence parameters, it seems 
> they are not typed as delegates inside a template, but are outside. I.e.
> 
> template T(V...) {
>      alias T = typeof(&V[0]);
> }
> 
> struct S { void f() {} }
> S s;
> 
> pragma(msg, T!(s.f)); // void function()
> pragma(msg, typeof(&s.f)); // void delegate()
> 
> How come the output is different? Is it supposed to be the same?

No, because the alias is an alias to the function, not the delegate.

The act of taking the address creates the delegate, where the delegate's 
ptr is the context pointer (i.e. s), and the funcptr is the function 
that accepts the pointer (i.e. S.f).

When you pass in s.f to an alias, you are actually passing in S.f. It's 
the fact that you are looking in the *namespace* of s when you do the 
alias. The &s.f is special for the compiler, and can't be deferred to later.

BUT, I'm thinking this may be fixable, as it's inconsistent with inner 
functions:

auto foo(alias x)() { return x(); }

struct S
{
    int bar() { return 42; }

    // int baz() { return foo!bar; } // nope
}

void main()
{
    S s;

    int bar() { return 42; }

    assert(foo!bar() == 42); // ok
    // assert(foo!(s.bar) == 42); // nope

    int baz() { return s.bar; }
    assert(foo!baz() == 42); // ok!
}

I don't see any reason why the alias is to the function and not the 
contexted function. I don't see how it's any different from the ones 
which use inner functions.

-Steve


More information about the Digitalmars-d-learn mailing list