Struct default constructor - need some kind of solution for C++ interop

Lodovico Giaretta via Digitalmars-d digitalmars-d at puremagic.com
Tue Sep 6 07:07:45 PDT 2016


On Tuesday, 6 September 2016 at 13:57:27 UTC, Lodovico Giaretta 
wrote:
> On Tuesday, 6 September 2016 at 13:44:37 UTC, Ethan Watson 
> wrote:
>> [...]
>> Suggestions?
>
> Of course I don't know which level of usability you want to 
> achieve, but I think that in this case your bind system, when 
> binding a default ctor, could use @disable this() and define a 
> factory method (do static opCall work?) that calls the C++ ctor.
>
> It's not as good-looking as a true default ctor, but it doesn't 
> provide any way to introduce bugs and it's not that bad (just a 
> couple key strokes).

Correcting my answer. The following code compiles fine:

struct S
{
     static S opCall()
     {
	S res = void;
	// call C++ ctor
	return res;
     }
}

void main()
{
     S s = S();
}

But introduces the possibility of using the default ctor 
inadvertitely.
Sadly, the following does not compile:

struct S
{
     @disable this();
     static S opCall()
     {
	S res = void;
	// call C++ ctor
	return res;
     }
}

Making this compile would solve your issues.


More information about the Digitalmars-d mailing list