Bug with offsetof?

Ali Çehreli acehreli at yahoo.com
Sun Nov 25 20:08:23 PST 2012


On 11/25/2012 07:23 PM, Geancarlo wrote:
> Hello, I'm using DMD32 D Compiler v2.060 for on Windows.
>
>
> module main;
>
> import std.stdio;
>
> int main(string[] argv)
> {
> writeln(TestStruct.x.offsetof);
> TestClass.test1();
> TestClass var = new TestClass();
> var.test2();
> return 0;
> }
>
> class TestClass
> {
> static void test1()
> {
> writeln(TestStruct.x.offsetof);
> }
>
> void test2()
> {
> writeln(TestStruct.x.offsetof);//bug here
> }
> }
>
> struct TestStruct
> {
> int x;
> }
>
> While test1 gives me no issues, test2 causes the following error:
>
> Error: this for x needs to be type TestStruct not type main.TestClass
>
>
> Is this a known bug? How can I work around this issue in order to use
> offsetof from a class function that is not static?
>
> Thanks

I don't know whether that is a bug but the class page at

   http://dlang.org/class.html

says ".offsetof can only be applied to expressions which produce the 
type of the field itself, not the class type".

Applying similar logic is a workaround for your case:

         writeln(TestStruct.init.x.offsetof);    // <-- works

As I said, I don't know whether it is a bug.

Ali


More information about the Digitalmars-d-learn mailing list