Thread overview
ref storage class in templates
Apr 16, 2008
Extrawurst
Apr 16, 2008
Janice Caron
Apr 16, 2008
Regan Heath
Apr 16, 2008
Robert Fraser
Apr 16, 2008
Christian Kamm
April 16, 2008
the documentation states that ref is a storage class for function parameters. just that this code piece wont compile cause the compiler doesn't. const in contrast works in templates too. so if no one has a veto i will file this as a bug for dmd.

[CODE]
void call(U...)(void delegate(U) dg, U args)
{
   dg(args);
}

void main()
{
   void plus(ref int x)
   {}

   int val = 2;
   call!(ref int)(&plus,val);

}
[/CODE]

dmd2.012: main.d(13): expression expected, not 'ref'
April 16, 2008
How is that a bug? I certainly wouldn't have expected that to compile. You can't pass "storage classes" to templates.
April 16, 2008
Janice Caron wrote:
> How is that a bug? I certainly wouldn't have expected that to compile.
> You can't pass "storage classes" to templates.

I suspect the confusion arises because 'const' is used as both a storage class and as a type modifier.

eg.

const int a = 5;  //storage class
void foo(const int a) {} //type modifier

and you can pass type modifiers to templates.

Regan
April 16, 2008
Extrawurst Wrote:
>     call!(ref int)(&plus,val);
> dmd2.012: main.d(13): expression expected, not 'ref'

As the others have pointed out, it is intended behavior: storage classes are only valid in function parameter declarations and thus can't be passed to a template as a type parameter.

You can pass 'ref int' within a type tuple using variadic template arguments for some reason, but I'm pretty sure that's useless (unless you resort to string mixins). See bugs 1818, 1411 and 1424 for related issues.

Christian

April 16, 2008
Regan Heath wrote:
> Janice Caron wrote:
>> How is that a bug? I certainly wouldn't have expected that to compile.
>> You can't pass "storage classes" to templates.
> 
> I suspect the confusion arises because 'const' is used as both a storage class and as a type modifier.
> 
> eg.
> 
> const int a = 5;  //storage class
> void foo(const int a) {} //type modifier
> 
> and you can pass type modifiers to templates.
> 
> Regan

We need to get rid of const the type modifier and keep const the type constructor as the only way to do it. That would solve the return-type const problem, too