[cfe-dev] Generating printf format specifiers

John McCall rjmccall at apple.com
Fri Jan 22 11:15:42 PST 2010


On Jan 22, 2010, at 10:35 AM, Dave Keck wrote:

> Hey list,
> 
> As an C/Obj-C developer, I've often thought that it would be useful to be able to do this:
> 
>    NSString *username = NSUserName();
>    uid_t userID = 5;
>    NSLog(@"Hello, " __nsfmt__(username) @"! This is your user id: " __nsfmt__(userID), username, userID);
> 
> I imagine __nsfmt__ would work similarly to __typeof__, but rather than substituting the type, the compiler would substitute the most natural printf format specifier for the given type. There would also be a standard-C __fmt__ that would generate constant C-strings rather than constant NSStrings. Is there any reason this doesn't already exist? (Or perhaps I've overlooked it if it does?)

It depends on what you mean by "substitute".  __typeof__ is not a macro;  it doesn't substitute tokens or anything, it actually gets parsed and interpreted by the semantic layer as a special kind of type-sugaring.  It looks like you want __fmt__ to work much more like a macro, since string-literal concatenation is still happening.  That's somewhat trickier and raises some interesting questions about behavior.

Also, we try to be very cautious about accepting new language enhancements, even in Objective C.  We'll really need to talk about whether we want this before we can accept it.

John.



More information about the cfe-dev mailing list