writef, compile-checked format, pointer

Bastiaan Veelo Bastiaan at Veelo.net
Mon Aug 9 22:27:07 UTC 2021


On Monday, 9 August 2021 at 22:01:18 UTC, Patrick Schluter wrote:
> On Monday, 9 August 2021 at 19:38:28 UTC, novice2 wrote:
>> format!"fmt"() and writef!"fmt"() templates
>> with compile-time checked format string
>> not accept %X for pointers,
>>
>> but format() and writef() accept it
>>
>> https://run.dlang.io/is/aQ05Ux
>> ```
>> void main() {
>>     import std.stdio: writefln;
>>     int x;
>>     writefln("%X", &x);  //ok
>>     writefln!"%s"(&x);  //ok
>>     //writefln!"%X"(&x);  //compile error
>> }
>> ```
>>
>> is this intentional?
>
> Yes. %X is to format integers.

It is to format pointers as well, according to the last table on 
https://dlang.org/phobos/std_format.html.

|Type|Format character|Formatted as|
|-|-|-|
|Pointer|	's'	|A null pointer is formatted as 'null'. All other 
pointers are formatted as hexadecimal numbers with the format 
character 'X'.|
| |'x', 'X'	|Formatted as a hexadecimal number.|

It looks like a bug to me.

— Bastiaan.




More information about the Digitalmars-d-learn mailing list