Thread overview
Writing UFCS function for any instance of a templated struct
Sep 13, 2012
Rene Zwanenburg
Sep 13, 2012
David
Sep 13, 2012
Rene Zwanenburg
Sep 13, 2012
bearophile
Sep 13, 2012
Rene Zwanenburg
September 13, 2012
Hi,

I'd like to write a function for a vector template which basically looks like

struct Vec(T, size_t size) {
  T[size] e;
}

This template has a few properties with very common names like x, y, z, etc. To avoid mistakes I'd like to write a template constraint for UFCS functions so it accepts any instance of the Vec template, but nothing else. Something in the gist of

auto innerProduct(T)(const T v1, const T v2) if(is(T == Vec)) {

}

How can I do this?
September 13, 2012
Am 13.09.2012 11:46, schrieb Rene Zwanenburg:
> Hi,
>
> I'd like to write a function for a vector template which basically looks
> like
>
> struct Vec(T, size_t size) {
>    T[size] e;
> }
>
> This template has a few properties with very common names like x, y, z,
> etc. To avoid mistakes I'd like to write a template constraint for UFCS
> functions so it accepts any instance of the Vec template, but nothing
> else. Something in the gist of
>
> auto innerProduct(T)(const T v1, const T v2) if(is(T == Vec)) {
>
> }
>
> How can I do this?

I am doing it in gl3n like this: https://github.com/Dav1dde/gl3n/blob/master/gl3n/util.d#L15

(Sorry when I just sent you a private e-mail, I clicked the wrong answer button in thunderbird)
September 13, 2012
That works, nice trick!

Thanks

On Thursday, 13 September 2012 at 10:09:08 UTC, David wrote:
> Am 13.09.2012 11:46, schrieb Rene Zwanenburg:
>> Hi,
>>
>> I'd like to write a function for a vector template which basically looks
>> like
>>
>> struct Vec(T, size_t size) {
>>   T[size] e;
>> }
>>
>> This template has a few properties with very common names like x, y, z,
>> etc. To avoid mistakes I'd like to write a template constraint for UFCS
>> functions so it accepts any instance of the Vec template, but nothing
>> else. Something in the gist of
>>
>> auto innerProduct(T)(const T v1, const T v2) if(is(T == Vec)) {
>>
>> }
>>
>> How can I do this?
>
> I am doing it in gl3n like this: https://github.com/Dav1dde/gl3n/blob/master/gl3n/util.d#L15
>
> (Sorry when I just sent you a private e-mail, I clicked the wrong answer button in thunderbird)


September 13, 2012
Rene Zwanenburg:

> How can I do this?

One possible solution:


struct Vec(T, size_t size) {
    T[size] e;
}
void innerProduct(T, size_t n)(const Vec!(T, n) v1,
                               const Vec!(T, n) v2) {}
void main() {
    Vec!(int, 3) v1, v2;
    innerProduct(v1, v2);
}


(While writing it I have found another little compiler bug).

Bye,
bearophile

September 13, 2012
That's actually quite straightforward. I forgot how well D's type deduction works.

Thanks

On Thursday, 13 September 2012 at 11:01:18 UTC, bearophile wrote:
> Rene Zwanenburg:
>
>> How can I do this?
>
> One possible solution:
>
>
> struct Vec(T, size_t size) {
>     T[size] e;
> }
> void innerProduct(T, size_t n)(const Vec!(T, n) v1,
>                                const Vec!(T, n) v2) {}
> void main() {
>     Vec!(int, 3) v1, v2;
>     innerProduct(v1, v2);
> }
>
>
> (While writing it I have found another little compiler bug).
>
> Bye,
> bearophile