std.format and uninitialized elements in CTFE

Steven Schveighoffer schveiguy at gmail.com
Thu Dec 5 19:45:27 UTC 2019


On 12/5/19 1:37 PM, 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

What is the point of formatting items that aren't initialized? You would 
be printing garbage.

What if you just initialize the items you wish to print, or if you have 
items that aren't initialized (but you don't want to print), create a 
toString overload? That's how I would handle it.

-Steve


More information about the Digitalmars-d mailing list