Struct fields and properties as alias members

Ben Jones fake at fake.fake
Wed Dec 8 17:36:53 UTC 2021

On Wednesday, 8 December 2021 at 17:29:19 UTC, H. S. Teoh wrote:
> On Wed, Dec 08, 2021 at 05:19:32PM +0000, Ben Jones via 
> Digitalmars-d-learn wrote:
>> I'm trying to use a property member of a struct as a template 
>> alias parameter and I don't really understand how to fix the 
>> error message I'm seeing (I also tried the simpler case of a 
>> plain struct member, and that didn't work either).  Is what 
>> I'm trying to do possible?  It seems like maybe I'd have to 
>> pass s as a runtime parameter to get it to work?
> It might help if you elaborate a bit more on what exactly 
> you're trying to achieve here.  Why can't you just pass the 
> value of the field to the function as a runtime parameter, for 
> example, which would be the usual way of doing it?  Is there 
> something specific you're trying to achieve here that requires 
> using a template parameter?
> T

It's a CPU simulator and the function needs to operate on half of 
a register (the CPU has 16 bit AF register, but I want to operate 
on A).  I've implemented the "A" part of the register as a pair 
of property functions:

@property ubyte a() const { return getHigh!af; }
@property ubyte a(ubyte val) { return setHigh!af(val); }

There are CPU instructions for each of the combined (AF, BC, DE, 
etc) and half(A, B, C, D, E, etc) registers, so I think either 
the half registers or the full registers would have to be 
implemented as property functions (from my reading of the spec, 
using unions seemed like technically it would be UB?)

Basically I want to write one template for the operation 
(increment, load, etc), and then instantiate it for each register 
(inc a, inc b, inc c, load a, load b, etc)

More information about the Digitalmars-d-learn mailing list