Opened 5 years ago

Closed 8 weeks ago

#166 closed defect (fixed)

Cannot get reference to generic member of generic struct

Reported by: mlbrooks Owned by: mlbrooks
Priority: major Component: cfa-cc
Version: 1.0 Keywords:
Cc:

Description (last modified by mlbrooks)

Minimal example:

forall( dtype T )
struct a {};

struct b1 {
    a(int) aa;
};

forall( dtype T )
struct b2 {
    a(T) aa;
};

int main() {
    #ifndef THE_ERROR
    b1 thingb;
    #else
    b2(int) thingb;
    #endif

    a(int) & thinga = thingb.aa;
}

On the error, message is:

CFA Version 1.0.0 (debug)
thebug.cfa: In function ‘_X4mainFi___1’:
thebug.cfa:21:36: error: lvalue required as unary ‘&’ operand
     a(int) & thinga = thingb.aa;

Using a pointer, in place of a reference, is often a workaround. This version compiles properly:

...
int main() {
    b2(int) thingb;
    a(int) * thinga = &thingb.aa;
}

However, the issue prevents defining a constructor for such a generic-of-generic struct. This declaration suffers from the same error.

forall( dtype T )
void ?{}( b2(T) & ) {}

A workaround for the constructor problem (which may often be hard to apply) is to make the b2 parameters sized. This version compiles.

forall( dtype T | sized(T) )
struct b2 {
    a(T) aa;
};

forall( dtype T | sized(T) )
void ?{}( b2(T) & ) {}

Change History (3)

comment:1 Changed 5 years ago by mlbrooks

Description: modified (diff)
Priority: majorminor
Summary: Cannot access inner member by reference when structs are genericCannot get reference to generic member of generic struct

comment:2 Changed 5 years ago by mlbrooks

Description: modified (diff)
Priority: minormajor

comment:3 Changed 8 weeks ago by ajbeach

Resolution: fixed
Status: assignedclosed

Fixed in: bdf4065045c0651b196411ff05d1d2c353dbfa1d

It might get overhauled, but right now the trick is to make sure we get the address and then cast instead of cast and then get the address (as that does not work in C).

Note: See TracTickets for help on using tickets.