VC++ macro definition ## operator

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



VC++ macro definition ## operator



I am trying to port our solution from VS2010 to VS2017 and have hit this compilation error with the macro definition involving token pasting operator. Here is the code below


#include “stdafx.h”

#define TEST_MACRO(str)

_T(“Error”) ## str ## _T(“””);


int main()

TEST_MACRO(“ check “);
return 0;



This compiles fine in VS2010 but fails in VS2017/VS2015. It doesn’t seem to recognize _T(“””) following str ##. I am using “multi byte character set” and hence _T(x) resolves to x ( in tchar.h ).



Can someone help me understand this issue?





for what you use _T at all ?
– RbMm
Aug 10 at 11:38


_T





Is it because you are using the “ char - replace with " Also you might need to #include <tchar.h> for the _T
– Angus Comber
Aug 10 at 11:42






what is sense of this macro at all ? what you try get ?
– RbMm
Aug 10 at 11:43





@Bathsheba - say this for microsoft. this is standard windows macro. exist large count of another such (leading underscore followed by a capital letter) macros and symbols
– RbMm
Aug 10 at 11:45





@RbMm: So long as you don't define it yourself, it's fine. Yes, didn't read the code in the question carefully enough. My bad.
– Bathsheba
Aug 10 at 11:46





3 Answers
3



Try this variant:


#define TEST_MACRO(str)

_T("Error") _T(str) _T(""");



Using ## you concatеnate your strings and for compiler it looks like: _T("Error")" check "_T("""). And " check "_T is a problem here. It is an user defined literal _T which is undefined


##


_T("Error")" check "_T(""")


" check "_T


_T





Thank you @Lex Sergeev. This solved the issue. Also using this expression with ## operator also seems to work fine. _T("Error") ## _T(str) ## _T(""").
– Tsst
Aug 10 at 13:09





Why is _T("Error") ## _T(str) ## _T(""") not an issue? _T(str) gets preprocessed to str which is "check", so the compiler should still see " check "_T
– Tsst
Aug 10 at 13:16


_T("Error") ## _T(str) ## _T(""")


" check "_T



You don't need the ##. Concatenating string literals is pointless.


##



"foo" "bar" "baz" is equivalent to "foobarbaz".


"foo" "bar" "baz"


"foobarbaz"



More likely it's stuck on the left double sharp as it tries to concatenate a closing parenthesis and str to produce a token. Have you tried to remove it?


str


#define TEST_MACRO(str) _T(“Error”) str ## _T(“””);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard