BetterC classes

Ilya Yaroshenko via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Dec 16 10:29:33 PST 2016


On Friday, 16 December 2016 at 16:24:18 UTC, Daniel N wrote:
> On Friday, 16 December 2016 at 15:17:15 UTC, Ilya Yaroshenko 
> wrote:
>> Hi
>>
>> Is it possible to use classes, which do not have monitor and 
>> other DRuntime stuff?
>>
>> Object can be allocated/deallocated using allocators, but they 
>> are very complex for betterC mode (monitor, mutex, object.d 
>> dependency).
>>
>> Can we have something more primitive?
>>
>> Ilya
>
> extern(c++)
> class
>
> Works pretty good in my experience, at least it gets rid of the 
> monitor, yay!

Thanks,
DRuntime is required anyway, but this is step forward.

ldmd2 -betterC -defaultlib= -O -inline -release -run cppclass.d

extern(C++) class Hw
{
	import core.stdc.stdio;

	this()
	{

	}

	void hw()
	{
		printf("hey\n");
	}
}

import std.traits;
extern(C)
int main()
{
	enum classSize = __traits(classInstanceSize, Hw);
	align(16)
	ubyte[classSize] payload = void;
	auto init = cast(const(ubyte)[]) typeid(Hw).initializer;
	foreach(i; 0..classSize)
		payload[i] = init[i];
	auto object = cast(Hw) payload.ptr;
	object.__ctor();
	object.hw();
	return 0;
}

-------------
Undefined symbols for architecture x86_64:
   "__D14TypeInfo_Class6__vtblZ", referenced from:
       __D8cppclass2Hw7__ClassZ in cppclass-7ed89bd.o


More information about the Digitalmars-d-learn mailing list