Bug 10522 – __FILE__ and other special keywords cannot be used with printf
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-07-01T15:00:00Z
Last change time
2013-08-19T08:35:40Z
Assigned to
nobody
Creator
andrej.mitrovich
Comments
Comment #0 by andrej.mitrovich — 2013-07-01T15:00:08Z
-----
import std.stdio;
void main()
{
printf("-- %s\n", __FILE__);
printf("-- %s\n", __FUNCTION__);
printf("-- %s\n", __PRETTY_FUNCTION__);
}
-----
> object.Error: Access Violation
The use-case are printf statements in destructors, which unlike writef will avoid any allocations (which typically fail in dtors due to the GC being stopped).
Comment #1 by yebblies — 2013-08-19T07:38:30Z
Neither can string literals! printf is a c-varargs function, and has no way of knowing you want the implicit conversion to const(char)*.
Solutions:
printf("-- %s\n", cast(const(char*))__FILE__);
printf("-- %.*s\n", __FILE__.length, __FILE__.ptr);
Comment #2 by andrej.mitrovich — 2013-08-19T08:35:40Z
(In reply to comment #1)
> Neither can string literals! printf is a c-varargs function, and has no way of
> knowing you want the implicit conversion to const(char)*.
I guess that shows how spoiled I am by D's variadic templates. :)
> printf("-- %s\n", cast(const(char*))__FILE__);
Also __FILE__.ptr and __FILE__.toStringz.