Bug 12252 – struct default constructors that execute code.
Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-02-25T06:56:00Z
Last change time
2014-02-28T05:07:29Z
Assigned to
nobody
Creator
remotion4d
Comments
Comment #0 by remotion4d — 2014-02-25T06:56:36Z
extern(C) int w_init(CWrapper *p);
extern(C) void w_free(CWrapper *p);
struct CWrapper {
this() { //not possible
w_init(&this);
}
~this() {
w_free(&this);
}
}
void main(){
S s; //should call w_init()
S s1 = S(); //should call w_init() too
}
While porting C++ code to D2 it is some times necessary to do be able to mimic C++ class/struct behavior.
Using this(int dummy){ ... } as a workaround compiles but does not work.
Other workarounds are complicated and error phone.
Comment #1 by bearophile_hugs — 2014-02-25T07:03:23Z
Converted to enhancement, because D is working as designed here (and from past discussions I have seen, this is at best a controversial change).
Comment #2 by yebblies — 2014-02-28T04:51:14Z
*** This issue has been marked as a duplicate of issue 3852 ***
Comment #3 by remotion4d — 2014-02-28T05:07:29Z
Ok then why any other workarounds for this problem do not work too ?
Here is one possible workaround that does not work.
struct S
{
@disable this();
@disable this(this);
this(int i){ }
static S opCall(){
S s = S(123);
return s;
}
}
This would help to find places in ported C++ code that need to be changed but it does not compiles.