www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - ptrdiff_t of class.tupleof entry

reply Satoshi <satoshi gshost.eu> writes:
Hello,
How can I take an ptrdiff_t of any tupleoff entry from class 
during compilation?


I need something like this:

template Test(T, int i) {
  enum Test = &T.tupleof[i] - &T.tupleof[0];
}


and then use it like:

class AnyClass {
int a;
int b;
int c;
string d;

}

enum Addr = Test!(AnyClass, 3);

void assignInt!(T)(AnyClass cls, T value) {
*(cast(T *)cast(void *)cls + Addr) = value;
}



This is just an example how I need to use it.
Is something like this possible to do?
Oct 10 2016
parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Monday, October 10, 2016 17:57:15 Satoshi via Digitalmars-d-learn wrote:
 Hello,
 How can I take an ptrdiff_t of any tupleoff entry from class
 during compilation?


 I need something like this:

 template Test(T, int i) {
   enum Test = &T.tupleof[i] - &T.tupleof[0];
 }


 and then use it like:

 class AnyClass {
 int a;
 int b;
 int c;
 string d;

 }

 enum Addr = Test!(AnyClass, 3);

 void assignInt!(T)(AnyClass cls, T value) {
 *(cast(T *)cast(void *)cls + Addr) = value;
 }



 This is just an example how I need to use it.
 Is something like this possible to do?
You can use the offsetof property of a member variable to find out the offset between its address and the address of the beginning of the class or struct that it's a member of. - Jonathan M Davis
Oct 10 2016
parent Satoshi <satoshi gshost.eu> writes:
On Monday, 10 October 2016 at 18:21:10 UTC, Jonathan M Davis 
wrote:
 On Monday, October 10, 2016 17:57:15 Satoshi via 
 Digitalmars-d-learn wrote:
 [...]
You can use the offsetof property of a member variable to find out the offset between its address and the address of the beginning of the class or struct that it's a member of. - Jonathan M Davis
Thanks a lot!
Oct 10 2016