Equivalent of C++ std::function

Yuushi via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 24 20:33:14 PDT 2014


On Wednesday, 25 June 2014 at 01:47:13 UTC, Mike Parker wrote:
> On 6/25/2014 10:45 AM, Mike Parker wrote:
>> On 6/25/2014 10:10 AM, Yuushi wrote:
>>> I was wondering if D had something akin to std::function in 
>>> C++.
>>>
>>> Say I have some functions in an associative array, for 
>>> example:
>>>
>>>      auto mapping = ['!' : (string a) => toUpper!string(a), 
>>> '^' :
>>> (string a) => capitalize!string(a)];
>>>
>>> What I want to do is basically declare something like:
>>>
>>>     function string(string) transform;
>>>     if(<some condition>) {
>>>          transform = mapping[<lookup>];
>>>     }
>>>
>>> In C++, this could be done by declaring:
>>>
>>>      std::function<string(string)> transform;
>>>
>>> Is there an equivalent D construct for this?
>>
>> For function pointers (free functions or static class 
>> functions):
>>
>> alias TransformFunc = string function( string );
>> TransformFunc transform;
>>
>> if( foo ) transform = &func;
>>
>> For delegates (lambdas or pointers to class methods or inner 
>> functions):
>>
>> alias TransformDg = string delegate( string );
>> TransformDG transform;
>>
>> if( foo ) transform = &bar.method;
>>
>
> And in this case you want the latter:
>
> TransformDg transform = mapping[ '!' ];
>
>
> ---
> This email is free from viruses and malware because avast! 
> Antivirus protection is active.
> http://www.avast.com

Thanks a ton - I guess I need to do a fair bit more reading about 
alias.


More information about the Digitalmars-d-learn mailing list