Macro, make wide string literal

J

Jim King

Given a macro which is a string literal. It cannot be changed since it
comes from some library.
#define ID_VERSION_STRING "5.3.3.98" // this macro cannot be
changed

I want a macro which is expanded as the wide character counterpart of
the string above.
#define IDW_VERSION_STRING ...ID_VERSION_STRING...
// make IDW_VERSION_STRING L"5.3.3.98"

Note: if ID_VERSION_STRING changes, I hope that IDW_VERSION_STRING
changes correspondingly.

How the definition of macro IDW_VERSION_STRING looks like?

Thanks.
 
R

Richard

[Please do not mail me a copy of your followup]

Jim King <[email protected]> spake the secret code
Given a macro which is a string literal. It cannot be changed since it
comes from some library.
#define ID_VERSION_STRING "5.3.3.98" // this macro cannot be
changed

I want a macro which is expanded as the wide character counterpart of
the string above.
#define IDW_VERSION_STRING ...ID_VERSION_STRING...
// make IDW_VERSION_STRING L"5.3.3.98"

Note: if ID_VERSION_STRING changes, I hope that IDW_VERSION_STRING
changes correspondingly.

How the definition of macro IDW_VERSION_STRING looks like?

If you're programming for the Windows API, include <tchar.h> and write
_T(ID_VERSION_STRING) when you want a wide or narrow string literal
depending on whether you compile ANSI or UNICODE.
 
J

Jim King

Hi Richard,

_T works. Thank you very much.

This can not work.
#define IDW_VERSION_STRING L ## ID_VERSION_STRING

error message from VS:
error C2065: 'LID_VERSION_STRING' : undeclared identifier

The point, WHY?
 
J

Jim King

The problem is that the macro 'ID_VERSION_STRING' isn't expanded when
'IDW_VERSION_STRING' is substituted.  It's an old idiosyncrasy of the C
preprocessor.  More preprocessor tricks are required.  Just use _T.

V

We are programming both on Windows and Mac OS.

I mimic _T macro on g++/Ubuntu. It works well.

#define __t(x) L ## x
#define _t(x) __t(x)

Interestingly, the __t must be defined before _t.

I'm very interested in these preprocessor tricks. Where can I find
them?

Thanks,
Jim
 
R

red floyd

We are programming both on Windows and Mac OS.

I mimic _T macro on g++/Ubuntu. It works well.

#define __t(x) L ## x
#define _t(x) __t(x)

Don't use __t. Any identifier with a double underscore is
reserved to the implementation; you may not declare it for
your own purposes.
 
R

Richard

[Please do not mail me a copy of your followup]

red floyd <[email protected]> spake the secret code
Don't use __t. Any identifier with a double underscore is
reserved to the implementation; you may not declare it for
your own purposes.

And if I recall correctly, any identifier beginning with an underscore
followed by a capital letter is also reserved.

Its easiest to just avoid creating your own identifiers (macros,
variables, members, functions, namespaces, etc.) that start with an
underscore. Include guards are the most frequent violators of that
recommendation (__INCLUDED_HEADER_H__).
 
R

red floyd

Richard said:
[Please do not mail me a copy of your followup]
red floyd <[email protected]> spake the secret code
<[email protected]> thusly:
And if I recall correctly, any identifier beginning with an underscore
followed by a capital letter is also reserved.

And while we're at it, any identifier with two consecutive underscores
is reserved. The example of __t is just a special (and quite common)
case of this broader rule.

Uh, Pete? That's what I said. :)
 
J

Jim King

Hi everyone,

Thank you for your excellent analysis and advices. I learned so much.

Regards,
Jim King
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,058
Members
48,769
Latest member
Clifft

Latest Threads

Top