View mode: basic / threaded / horizontal-split · Log in · Help
November 26, 2012
Bug with offsetof?
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
November 26, 2012
Re: Bug with offsetof?
This also works fine:

	void test3()
	{
		TestStruct dummy;
		writeln(dummy.x.offsetof);
	}
November 26, 2012
Re: Bug with offsetof?
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.
November 26, 2012
Re: Bug with offsetof?
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
November 26, 2012
Re: Bug with offsetof?
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.
November 26, 2012
Re: Bug with offsetof?
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
November 26, 2012
Re: Bug with offsetof?
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 here

This will create an instance of TestStruct.

-- 
/Jacob Carlborg
November 26, 2012
Re: Bug with offsetof?
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 here
> 

This works without creating your own instance:
          writeln(TestStruct.init.x.offsetof);

Thanks
Dan
Top | Discussion index | About this forum | D home