Question

I have some code I am maintaining that I've started compiling under clang 3.3. When compiling with "-std=c++11", clang generates an error (given below). I've distilled the offending code to the following:

#include <stdio.h>

#define DBG_PRT(__format, ...) \
         printf("%s:%d:%s: "__format, __FILE__, \
                       __LINE__, __FUNCTION__, ## __VA_ARGS__)

int main()
{
    DBG_PRT("%s\n", "Hi");
}

This is clang's output:

test.cpp:10:5: error: no matching literal operator for call to 'operator "" __format' with arguments of types 'const char *' and 'unsigned int'

DBG_PRT("%s\n", "Hi");

^ test.cpp:4:29: note: expanded from macro 'DBG_PRT'
     printf("%s:%d:%s: "__format, __FILE__, \
                        ^ 1 error generated.

Without spaces between the string literal and "__format", it doesn't seem like the preprocessor should be able to expand __format. It clearly is, though, when not specifying -std=c++11. G++ 4.4.7 (with and without -std=c++0x) compiles just fine.

Is there an error with the compiler?

Was it helpful?

Solution

This is because ""_ is a syntax for user-defined string literals. Put a space in between to have the old behavior (concatenate literals). GCC works fine because 4.4.7 does not implement user defined literals (it appeared in version 4.7).

Also, as @Fred have pointed out, try to avoid using reserved identifier (double underscore).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top