template instance cannot use local 'f' as parameter to non-global template

Tyro[a.c.edwards] nospam at home.com
Wed Jul 13 18:44:30 PDT 2011


On 7/14/2011 12:24 AM, Trass3r wrote:
> Am 13.07.2011, 16:58 Uhr, schrieb Tyro[a.c.edwards] <nospam at home.com>:
>> Don't know it this is the right answer or a possible bug but it does
>> the trick:
>>
>> void h() { import std.stdio; write("h()"); }
>>
>> class Bla
>> {
>> mixin wrap!h;
>> }
>>
>> mixin template wrap(alias f)
>> {
>> void blub(typeof(&f) g = &f)
>> {
>> g();
>> }
>> }
>>
>> void main()
>> {
>> Bla b = new Bla();
>> b.blub();
>> }
>
> Thanks!
> Unfortunately it doesn't work with more complex functions:
>
> Error: arithmetic/string type expected for value-parameter, not
> cl_errcode C function(cl_program program, uint param_name, ulong
> param_value_size, void* param_value, ulong* param_value_size_ret)

I guess the simplest example of the problem you're experiencing would be 
this:

void h() { import std.stdio; write("h()"); }

void function() fp = &h;

class Bla
{
     mixin wrap!(fp);
}

mixin template wrap(alias f)
{
     void blub()
     {
         typeof(&f) g = &f;
     g();  // <--- source of error [1]
     }
}

void main()
{
     Bla b = new Bla();
     b.blub();
}

edit1.d(19): Error: function expected before (), not g of type void 
function()*

[1] Here you are calling a function pointer which simply returns the 
address of the function... hence your error!

Try calling the function pointed to by differencing the pointer as such: 
(*g)() and your problem is solved.


More information about the Digitalmars-d-learn mailing list