www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - ref storage class in templates

reply Extrawurst <spam extrawurst.org> writes:
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'
Apr 16 2008
next sibling parent reply "Janice Caron" <caron800 googlemail.com> writes:
How is that a bug? I certainly wouldn't have expected that to compile.
You can't pass "storage classes" to templates.
Apr 16 2008
parent reply Regan Heath <regan netmail.co.nz> writes:
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
Apr 16 2008
parent Robert Fraser <fraserofthenight gmail.com> writes:
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
Apr 16 2008
prev sibling parent Christian Kamm <kamm.incasoftware shift_at_left.de> writes:
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
Apr 16 2008