RAII trouble
anonymous via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Nov 20 15:21:02 PST 2015
On 20.11.2015 23:56, Spacen Jasset wrote:
> The ideal would be to have a struct that can be placed inside a function
> scope, or perhaps as a module global variable. Why does Ft_Init_FreeType
> have to be read at compile time?
>
>
> text.d(143,15): Error: static variable FT_Init_FreeType cannot be read
> at compile time
> text.d(174,43): called from here: initialise()
>
> struct FreeType
> {
> @disable this();
>
> static FreeType initialise()
> {
> return FreeType(0);
> }
>
> this(int)
> {
> int error = FT_Init_FreeType(&library); /// ERROR
> enforce(!error);
> }
>
> alias library this;
>
> ~this()
> {
> FT_Done_FreeType(library);
> }
> FT_Library library;
> }
>
> FreeType freeType = FreeType.initialise();
>
FT_Init_FreeType must be read at compile time, because freeType is a
module level variable, and initializers for module variables must be
static values. The initializer is run through CTFE (Compile Time
Function Evaluation).
Put the initialization/assignment in a function and it should work. That
function can be a static constructor or the main function:
----
FreeType freeType = void; /* 'void' prevents default initialization */
static this() {freeType = FreeType.initialise();} /* should work */
void main() {freeType = FreeType.initialise();} /* too */
----
More information about the Digitalmars-d-learn
mailing list