std.format and uninitialized elements in CTFE

Ali Çehreli acehreli at yahoo.com
Thu Dec 5 20:52:21 UTC 2019


On 12/5/19 10:37 AM, berni44 wrote:
> std.format contains code, that accesses the members of a struct via 
> tupleof (which gives access to private members, that might be 
> intentionally not yet initialized), to print them. If members are not 
> initialized it produces a "used before initialized" error or something 
> similar. (See issue 19769 [1]).
> 
> What to do about this? Should std.format check if the members of a 
> struct are not initialized and do what in that case? And how to check them?
> 
> Here an example:
> 
> ```
> import std.format;
> 
> struct Foo
> {
>      int a = void;
> }
> 
> static x = format("%s", Foo());
> ```
> 
> Replace int by int[3] for a more complex version.
> 
> [1] https://issues.dlang.org/show_bug.cgi?id=19769

Similar:

import std.string;

struct S {
   int i = void;
}

void main() {
   auto s = S(42);
   auto f = format!"%s"(s);
}

Error: cannot read uninitialized variable `i` in CTFE

Note that the user is not doing anything at compile time. Only format is 
attempting to catch errors in the format string.

Ali



More information about the Digitalmars-d mailing list