UFCS functions with both pointers and refs

Mike Parker aldacron at gmail.com
Wed Dec 16 03:50:07 UTC 2020


On Sunday, 13 December 2020 at 19:02:34 UTC, Dave P. wrote:
> On Sunday, 13 December 2020 at 18:44:20 UTC, Mike Parker wrote:
>> On Sunday, 13 December 2020 at 18:31:54 UTC, Dave P. wrote:
>>> Do I have to write both and have one forward to the other for 
>>> more
>>> complicated functions?
>>
>> For free functions, yes.
>
> Is there any way to write the function as a template that is 
> generic over a parameter being a pointer or a reference, but 
> does not allow passing a copy?

Even with a templated function, you'd still need to declare the 
first parameter to the function either as a pointer or as a `ref` 
parameter. I'm unaware of any way to get around that. Free 
functions and member functions are just different beasts

Based on you requirement to use pointers, I assume you're doing 
this for a type you get from a C library. I did the same thing 
for the SDL_Rect type when working with SDL. All I did was 
implement a pointer version of my "extension" functions. When I 
had an instance and not a pointer, I took its address when I 
called the function. If that's inconvenient (lots of generic 
code, perhaps), you can always have the pointer version forward 
to the ref version, but it seems to me like just having the one 
version is the way to go most of the time.





More information about the Digitalmars-d-learn mailing list