digitalmars.D - Class Field Size/Offsets
- Maxime Chevalier (13/13) Mar 02 2013 To access class fields in machine code generated by my JIT
- bearophile (6/8) Mar 02 2013 I have related ER since a lot of time that has not received an
- Andrej Mitrovic (10/14) Mar 02 2013 Shouldn't the following work for you?
- Maxime Chevalier (19/19) Mar 02 2013 Your example works but my use case doesn't and I have no idea why:
- Maxime Chevalier (16/16) Mar 02 2013 The problem persists without the mixin and the template:
- Walter Bright (20/21) Mar 02 2013 It compiles without complaint for this code:
- Maxime Chevalier (2/2) Mar 02 2013 I'm guessing you use a bleeding edge version in which the bug has
- Peter Alexander (2/4) Mar 02 2013 Ditto: http://dpaste.dzfl.pl/8bf3cc73
- Walter Bright (2/4) Mar 02 2013 You're correct.
- Andrej Mitrovic (18/34) Mar 02 2013 For 2.062 you're going to have to move the .sizeof expression
- Maxime Chevalier (4/4) Mar 02 2013 Ok. I just got extra confused because in addition to the bug,
To access class fields in machine code generated by my JIT compiler, I need to be able to know the offset and size of the said fields. It seems that D requires a pointer to an instance of the class to make this work. This seems rather problematic for my usage. Is it safe to circumvent this by using a null pointer or could I potentially get an exception/segfault by doing this? E.g.: MyClass ptr = null; auto fSize = ptr.theField.sizeof; auto fOffs = ptr.theField.offsetof; Is it safe to assume that the offset is the same in every instance? I would assume so (why would the offsets change?) but I'd like to know if the language actually guarantees this.
Mar 02 2013
Maxime Chevalier:It seems that D requires a pointer to an instance of the class to make this work.I have related ER since a lot of time that has not received an answer in about three years: http://d.puremagic.com/issues/show_bug.cgi?id=3939 Bye, bearophile
Mar 02 2013
On 3/2/13, Maxime Chevalier <maximechevalierb gmail.com> wrote:To access class fields in machine code generated by my JIT compiler, I need to be able to know the offset and size of the said fields. It seems that D requires a pointer to an instance of the class to make this work.Shouldn't the following work for you? class C { int a; int[4] b; } void main() { pragma(msg, C.a.sizeof); pragma(msg, C.a.offsetof); pragma(msg, C.b.sizeof); pragma(msg, C.b.offsetof); }
Mar 02 2013
Your example works but my use case doesn't and I have no idea why:
class C { int a; }
struct Foo
{
void foo(string className, string fieldName)()
{
mixin("auto sz = " ~ className ~ "." ~ fieldName ~
".sizeof;");
writefln("sz: %s", sz);
}
}
static this()
{
Foo f;
f.foo!("C", "a");
}
Produces:
Error: this for a needs to be type C not type Foo
Error: template instance Foo.foo!("C", "a") error instantiating
Mar 02 2013
The problem persists without the mixin and the template:
class C { int a; }
struct Foo
{
void foo()
{
auto sz = C.a.sizeof;
writefln("sz: %s", sz);
}
}
static this()
{
Foo f;
f.foo();
}
Error: this for a needs to be type C not type Foo
Mar 02 2013
On 3/2/2013 2:10 PM, Maxime Chevalier wrote:The problem persists without the mixin and the template:It compiles without complaint for this code: -------------------------------------------- import std.stdio; class C { int a; } struct Foo { void foo() { auto sz = C.a.sizeof; writefln("sz: %s", sz); } } static this() { Foo f; f.foo(); } void main() { } -----------------------------------------
Mar 02 2013
I'm guessing you use a bleeding edge version in which the bug has been fixed then. It doesn't work on DMD64 v2.062.
Mar 02 2013
On Saturday, 2 March 2013 at 22:25:24 UTC, Maxime Chevalier wrote:I'm guessing you use a bleeding edge version in which the bug has been fixed then. It doesn't work on DMD64 v2.062.Ditto: http://dpaste.dzfl.pl/8bf3cc73
Mar 02 2013
On 3/2/2013 2:25 PM, Maxime Chevalier wrote:I'm guessing you use a bleeding edge version in which the bug has been fixed then. It doesn't work on DMD64 v2.062.You're correct.
Mar 02 2013
On Saturday, 2 March 2013 at 22:10:57 UTC, Maxime Chevalier wrote:
The problem persists without the mixin and the template:
class C { int a; }
struct Foo
{
void foo()
{
auto sz = C.a.sizeof;
writefln("sz: %s", sz);
}
}
static this()
{
Foo f;
f.foo();
}
Error: this for a needs to be type C not type Foo
For 2.062 you're going to have to move the .sizeof expression
outside of any methods which require 'this'. Make 'foo' static or
wrap the .sizeof expression inside of a template and it will
work. E.g.:
template getSizeOf(alias symb)
{
enum getSizeOf = symb.sizeof;
}
struct Foo
{
void foo()
{
auto sz = getSizeOf!(C.a);
writefln("sz: %s", sz);
}
}
It's just a bug which will be fixed in the next version.
Mar 02 2013
Ok. I just got extra confused because in addition to the bug, this page: http://dlang.org/class.html (Field Properties section) seems to imply that C.a.sizeof isn't valid. Someone should probably update the page to reflect the proper behavior.
Mar 02 2013









"bearophile" <bearophileHUGS lycos.com> 