www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template reference parameter

reply "rumbu" <rumbu rumbu.ro> writes:
I'm trying to construct a struct template where one of the 
template parameters is passed by reference (as option)

struct S(T, U)
{
    void opCall(T t, U u) { }
}

alias X = S!(T, U);
alias RX = S!(T, ref U); //not working

The objective is  to call X(t, u) or RX(t, u), but in second case 
I want "u" to be passed by reference.

The only solution I found is to pass U as a pointer or as a class.
Mar 23 2015
parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Mon, 23 Mar 2015 22:01:03 +0000, rumbu wrote:

 I'm trying to construct a struct template where one of the template
 parameters is passed by reference (as option)
=20
 struct S(T, U)
 {
     void opCall(T t, U u) { }
 }
=20
 alias X =3D S!(T, U);
 alias RX =3D S!(T, ref U); //not working
=20
 The objective is  to call X(t, u) or RX(t, u), but in second case I want
 "u" to be passed by reference.
=20
 The only solution I found is to pass U as a pointer or as a class.
`ref` is not a part of a type, so you can't do it like this. you have to=20 have two different templates for that. or try to use `auto ref`, it may=20 help.=
Mar 23 2015
parent reply "rumbu" <rumbu rumbu.ro> writes:
On Monday, 23 March 2015 at 22:13:37 UTC, ketmar wrote:
 On Mon, 23 Mar 2015 22:01:03 +0000, rumbu wrote:

 I'm trying to construct a struct template where one of the 
 template
 parameters is passed by reference (as option)
 
 struct S(T, U)
 {
     void opCall(T t, U u) { }
 }
 
 alias X = S!(T, U);
 alias RX = S!(T, ref U); //not working
 
 The objective is  to call X(t, u) or RX(t, u), but in second 
 case I want
 "u" to be passed by reference.
 
 The only solution I found is to pass U as a pointer or as a 
 class.
`ref` is not a part of a type, so you can't do it like this. you have to have two different templates for that. or try to use `auto ref`, it may help.
I have only one template and I think it's impossible to define specializations since it's supposed that a random member of A... will be passed sometime by reference: struct MulticastDelegate(ReturnType, A...) { //... ReturnType opCall(A args) { //... } } Here it's what I have right now: alias SomeDelegateWithInt = MulticastDelegate!(bool, char, int); alias SomeDelegateWithRefInt = MulticastDelegate!(bool, char, int*); SomeDelegateWithInt d1; SomeDelegateWithRefInt d2; ... int value; bool b = d1('a', value); bool b = d2('b', &value); It's working, but I don't like the &value thing, ref would be better.
Mar 23 2015
next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
On Mon, 23 Mar 2015 22:39:27 +0000, rumbu wrote:

 It's working, but I don't like the &value thing, ref would be better.
alas. you can write a complex mixin that will generate the appropriate=20 templates for you, though, so you'll be able to do something like this: mixin(MulticastDelegate!(`withRefInt`, `bool`, `char`, `ref int`)); but it will be error-prone and ugly. so the best thing is to stick with=20 pointers.=
Mar 23 2015
prev sibling parent reply "weaselcat" <weaselcat gmail.com> writes:
On Monday, 23 March 2015 at 22:39:28 UTC, rumbu wrote:

 I have only one template and I think it's impossible to define 
 specializations since it's supposed that a random member of 
 A... will be passed sometime by reference:
auto ref? http://dlang.org/template.html#auto-ref-parameters
Mar 23 2015
parent ketmar <ketmar ketmar.no-ip.org> writes:
On Tue, 24 Mar 2015 00:54:12 +0000, weaselcat wrote:

 On Monday, 23 March 2015 at 22:39:28 UTC, rumbu wrote:
=20
 I have only one template and I think it's impossible to define
 specializations since it's supposed that a random member of A... will
 be passed sometime by reference:
=20 auto ref? http://dlang.org/template.html#auto-ref-parameters
nope, that is not what OP wants. OP's wish is impossible, due to `ref`=20 not being a part of a type.=
Mar 23 2015