Calling template member function?

Steven Schveighoffer schveiguy at gmail.com
Wed Apr 20 02:05:28 UTC 2022


On 4/19/22 8:44 PM, Andrey Zherikov wrote:
> On Tuesday, 19 April 2022 at 20:29:01 UTC, Steven Schveighoffer wrote:
>> You can work around the dual context, if you are OK with passing the 
>> second context explicitly.
>>
>> The easiest way is to move the member function to a UFCS function. an 
>> example:
>>
>> ```d
>> struct X
>> {
>>    int x;
>>    void applyToX(alias fn)() {fn(x);}
>> }
>>
>> void applyToX_alt(alias fn)(ref X xval) {
>>    fn(xval.x);
>> }
>>
>> void main()
>> {
>>    auto s = X(5);
>>    int y = 6;
>>    void fn(ref int x) { x += y; }
>>    s.applyToX!fn; // error, dual context needed
>>    s.applyToX_alt!fn; // fine, only single context needed
>> }
>> ```
> 
> I used struct to understand the problem. I don't actually have an object 
> context to pass like in your example, the only context I have is 
> template parameters.
> 

That's because your code is not an example of the dual context problem 
-- only one context is needed. Your code is triggering an actual bug in 
the compiler.

-Steve


More information about the Digitalmars-d-learn mailing list