digitalmars.D.learn - Bug with offsetof?
- Geancarlo (32/32) Nov 25 2012 Hello, I'm using DMD32 D Compiler v2.060 for on Windows.
- Geancarlo (6/6) Nov 25 2012 This also works fine:
- jerro (40/40) Nov 25 2012 This works for me if I add parentheses to the line where you get
- Jacob Carlborg (4/7) Nov 25 2012 This will create an instance of TestStruct.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (9/40) Nov 25 2012 I don't know whether that is a bug but the class page at
- Geancarlo (4/4) Nov 25 2012 Thanks jerro and Ali, I see your points. I thought offsetof was
- Jacob Carlborg (4/7) Nov 25 2012 You do have .sizeof in D as well.
- Dan (5/8) Nov 26 2012 This works without creating your own instance:
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
Nov 25 2012
This also works fine: void test3() { TestStruct dummy; writeln(dummy.x.offsetof); }
Nov 25 2012
This works for me if I add parentheses to the line where you get the error like this: writeln(TestStruct().x.offsetof);//bug here The error you were getting is not related to offsetof. The problem seems to be that if you write TestStruct.x inside a non-static method, the compiler thinks you are trying to get member TestStruct.x of the current instance. You obviously can't do that because the current instance is not a TestStruct. I've never used this feature, but it seems you can access members like this: class Foo { int x = 42; void test() { writeln(Foo.x); // prints 42 } } Doing this seems pretty pointless, though. I assume the reason behind this is to allow you to access the members of a superclass that are named the same as current classes members, like this: class Parent { int x = 1; } class Child : Parent { int x = 2; void test() { writeln(x); writeln(Parent.x); } } (new Child).test() prints: 2 1 When you add parentheses after TestStruct, you create an instance of TestStruct, and then you access its member x, so there is no ambiguity.
Nov 25 2012
On 2012-11-26 05:03, jerro wrote:This works for me if I add parentheses to the line where you get the error like this: writeln(TestStruct().x.offsetof);//bug hereThis will create an instance of TestStruct. -- /Jacob Carlborg
Nov 25 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? ThanksI 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
Nov 25 2012
Thanks jerro and Ali, I see your points. I thought offsetof was like C/C++'s sizeof... Guess while taking a crash course at a new language I will often bump into issues because I haven't read a specific doc.
Nov 25 2012
On 2012-11-26 05:49, Geancarlo wrote:Thanks jerro and Ali, I see your points. I thought offsetof was like C/C++'s sizeof... Guess while taking a crash course at a new language I will often bump into issues because I haven't read a specific doc.You do have .sizeof in D as well. -- /Jacob Carlborg
Nov 25 2012
On Monday, 26 November 2012 at 03:23:42 UTC, Geancarlo wrote:Hello, I'm using DMD32 D Compiler v2.060 for on Windows.writeln(TestStruct.x.offsetof);//bug hereThis works without creating your own instance: writeln(TestStruct.init.x.offsetof); Thanks Dan
Nov 26 2012