www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Help?

reply Manu <turkeyman gmail.com> writes:
struct MyStruct; // <- forward declared (opaque type, never defined)

MyStruct*[] arrayOfPointers;

arrayOfPointers ~= null; // appending doesn't work
arrayOfPointers = new MyStruct*[n]; // or just allocating the array doesn't
work either

Complains:
1>code.d(84): Error: struct MyStruct is forward referenced when looking for
'toHash'
1>code.d(84): Error: struct MyStruct is forward referenced when looking for
'opCmp'
1>code.d(84): Error: struct MyStruct is forward referenced when looking for
'toString'
1>code.d(84): Error: struct MyStruct unknown size
1>code.d(84): Error: struct MyStruct no size yet for forward reference
1>code.d(84): Error: struct MyStruct unknown size
1>code.d(84): Error: struct MyStruct no size yet for forward reference

What's the go here?
Why would it need any of that information? It's just a pointer...
Aug 30 2013
next sibling parent reply "Dicebot" <public dicebot.lv> writes:
Is it even legal D? I though forward declaration was allowed only 
for "extern(C)" stuff.
Aug 30 2013
next sibling parent reply Manu <turkeyman gmail.com> writes:
So should I extern(C) the forward declaration?


On 31 August 2013 03:45, Dicebot <public dicebot.lv> wrote:

 Is it even legal D? I though forward declaration was allowed only for
 "extern(C)" stuff.

Aug 30 2013
parent reply Johannes Pfau <nospam example.com> writes:
Am Sat, 31 Aug 2013 04:09:10 +1000
schrieb Manu <turkeyman gmail.com>:

 So should I extern(C) the forward declaration?

Sometimes --- struct MyStruct {} --- is used as a workaround, but that may have other issues.
Aug 30 2013
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 8/30/13, Johannes Pfau <nospam example.com> wrote:
 Am Sat, 31 Aug 2013 04:09:10 +1000
 schrieb Manu <turkeyman gmail.com>:

 So should I extern(C) the forward declaration?

Sometimes --- struct MyStruct {} ---

Yeah opaque structs have their bugs, I currently use this workaround in dlibgit: struct MyStruct { disable this(); disable this(this); } This should be close to an opaque type, although .sizeof might still work, so it's not 100% opaque.
Aug 30 2013
prev sibling parent Manu <turkeyman gmail.com> writes:
On 31 August 2013 04:59, Johannes Pfau <nospam example.com> wrote:

 Am Sat, 31 Aug 2013 04:09:10 +1000
 schrieb Manu <turkeyman gmail.com>:

 So should I extern(C) the forward declaration?

Sometimes --- struct MyStruct {} --- is used as a workaround, but that may have other issues.

Yeah, that's no good. That certainly opens up more problems. The point is that it is an undefined struct, and it can only be used to type a pointer. It's pretty much impossible to extern to a lot of C code if this doesn't work properly.
Aug 31 2013
prev sibling parent "Mike Parker" <aldacron gmail.com> writes:
On Friday, 30 August 2013 at 17:45:14 UTC, Dicebot wrote:
 Is it even legal D? I though forward declaration was allowed 
 only for "extern(C)" stuff.

My understanding of extern(C) is that it just affects the calling convention (and, I assume, name mangling) of functions/function pts and has no impact on types. I haven't seen anything in the documentation to tell me otherwise.
Aug 30 2013
prev sibling next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 08/30/2013 10:27 AM, Manu wrote:
 struct MyStruct; // <- forward declared (opaque type, never defined)

 MyStruct*[] arrayOfPointers;

 arrayOfPointers ~= null; // appending doesn't work
 arrayOfPointers = new MyStruct*[n]; // or just allocating the array doesn't
 work either

 Complains:
 1>code.d(84): Error: struct MyStruct is forward referenced when looking for
 'toHash'
 1>code.d(84): Error: struct MyStruct is forward referenced when looking for
 'opCmp'
 1>code.d(84): Error: struct MyStruct is forward referenced when looking for
 'toString'
 1>code.d(84): Error: struct MyStruct unknown size
 1>code.d(84): Error: struct MyStruct no size yet for forward reference
 1>code.d(84): Error: struct MyStruct unknown size
 1>code.d(84): Error: struct MyStruct no size yet for forward reference

 What's the go here?
 Why would it need any of that information? It's just a pointer...

The effect on Variant: http://d.puremagic.com/issues/show_bug.cgi?id=10766 Ali
Aug 30 2013
prev sibling parent "Mike Parker" <aldacron gmail.com> writes:
On Friday, 30 August 2013 at 17:28:12 UTC, Manu wrote:
 struct MyStruct; // <- forward declared (opaque type, never 
 defined)

 MyStruct*[] arrayOfPointers;

 arrayOfPointers ~= null; // appending doesn't work
 arrayOfPointers = new MyStruct*[n]; // or just allocating the 
 array doesn't
 work either

 Complains:
 1>code.d(84): Error: struct MyStruct is forward referenced when 
 looking for
 'toHash'
 1>code.d(84): Error: struct MyStruct is forward referenced when 
 looking for
 'opCmp'
 1>code.d(84): Error: struct MyStruct is forward referenced when 
 looking for
 'toString'
 1>code.d(84): Error: struct MyStruct unknown size
 1>code.d(84): Error: struct MyStruct no size yet for forward 
 reference
 1>code.d(84): Error: struct MyStruct unknown size
 1>code.d(84): Error: struct MyStruct no size yet for forward 
 reference

 What's the go here?
 Why would it need any of that information? It's just a 
 pointer...

http://d.puremagic.com/issues/show_bug.cgi?id=10451 A couple of other forward-ref bugs relating to opaque structs have been recently fixed in git master. If I knew where to look or what to do, I'd fix this one myself. It's an ongoing source of bug reports for Derelict and is bugging the hell out of me.
Aug 30 2013