[cfe-dev] invalid-token-paste

James Dennett james.dennett at gmail.com
Wed Jul 18 18:24:08 PDT 2012


On Wed, Jul 18, 2012 at 5:58 PM, Shang Yu <yusunn at gmail.com> wrote:
> Hi dear all,
> Look at the following code
>
> #include <stdio.h>
> #define STR(a,b) a##b
> void test()
> {
>         printf("%s\n" , STR("1","2"));
> }

That's invalid code.  "1""2" is not a valid token.  Fortunately, you
don't need it to be -- adjacent string literals are implicitly
concatenated in a later phase of translation.  You could just use
  #define STR(a,b) a b
or drop the macro and write
  printf("%s\n", "1" "2");

> compiled with clang -c test2.cpp will failed with following error:
> test2.cpp:9:18: error: pasting formed '"1""2"', an invalid preprocessing token
>       [-Winvalid-token-paste]
>         printf("%s\n" , STR("1","2"));
>                         ^
> test2.cpp:6:19: note: expanded from macro 'STR'
> #define STR(a,b) a##b
>                   ^
> 1 error generated.
>                   ^

That looks like a good error message to me -- can you suggest any way
to make it clearer, or point out any problems with it?

> the same code will pass compiled with msvc. Any suggestion ? Many thanks !

I'd suggest filing a bug against MSVC, but it's not all that important
-- MSVC will likely accept the corrected code, as will all conforming
compilers.

-- James



More information about the cfe-dev mailing list