Thread overview
[Issue 10522] New: __FILE__ and other special keywords cannot be used with printf
Jul 01, 2013
Andrej Mitrovic
Aug 19, 2013
yebblies
Aug 19, 2013
Andrej Mitrovic
July 01, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10522

           Summary: __FILE__ and other special keywords cannot be used
                    with printf
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrej.mitrovich@gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-07-01 15:00:08 PDT ---
-----
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).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 19, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10522


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |yebblies@gmail.com
         Resolution|                            |INVALID


--- Comment #1 from yebblies <yebblies@gmail.com> 2013-08-20 00:38:30 EST ---
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);

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 19, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10522



--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-08-19 08:35:40 PDT ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------