Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6969307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:33:52+00:00 2026-05-27T16:33:52+00:00

I have some code that needs Unicode strings for the most part, but I

  • 0

I have some code that needs Unicode strings for the most part, but I want to make it conditional (ie, TEXT("string") expanding to L"string" or "string", depending on settings). For these, I use the macro:

#ifdef _UNICODE
#    define VSTR(str) L##str
#else
#    define VSTR(str) str
#endif

The main complication with this is printf format strings, which use %s and %S for same-encoding and other-encoding strings, respectively. Some strings come from similarly conditional APIs (TCHAR and similar), while some come from set APIs (mostly C-string only). When using _tprintf and family, the function used can vary making %s and %S conditional as well, and they may need to be flipped around. To handle this, I defined macros to the appropriate format elements:

#ifdef _UNICODE
#    define VPFCSTR(str) "%S"
#    define VPFWSTR(str) "%s"
#    define VPFTSTR(str) VPFWSTR(str)
#else
#    define VPFCSTR(str) "%s"
#    define VPFWSTR(str) "%S"
#    define VPFTSTR(str) VPFCSTR(str)
#else

Now, this all works fine, but forces the syntax:

VSTR("Beginning of a format string, with a string '") VPFTSTR VSTR("' included.")

I would like to be able to use a syntax like:

VSTR("Beginning of a format string, with a string '", VPFTSTR, "' included.")

For Unicode, this needs to expand to:

L"Beginning of a format string, with a string '" L"%s" L"' included."

The only complication is the variable number of arguments, all of which need to be transformed in the same manner (one by one, if necessary).

My first idea was to use __VA_ARGS__ to handle this, using empty arguments, like:

VASTR(str, ...) VSTR(str) VASTR(__VA_ARGS__)

Unfortunately, as macros can’t be used in their own definition, this fails. I then attempted a proxy:

VASTR2(...) VASTR(__VA_ARGS__)
VASTR(str, ...) VSTR(str) VASTR2(__VA_ARGS__)

The proxy method doesn’t appear to work either.

Is there a way to handle running the same macro on each argument of a(nother) macro, which takes a variable number of arguments? Or, if not, is there an equivalent? If compiler-specific, MSVC10 is preferred, but anything is of interest.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:33:52+00:00Added an answer on May 27, 2026 at 4:33 pm

    Recursive macro expansion is not possible in C/C++.

    Not sure, but C++0x allows you to omit encoding prefix for string literal concatenation. Thus you can try design your macro to prepend L only to the first string literal and use it as follows:

    VSTR("Beginning of a format string, with a string '" VPFTSTR "' included.")
    

    Please, correct me, if I’m wrong.

    UPD.

    A similar Unicode-related question: What happens with adjacent string literal concatenation when there is a modifier(L, u8, etc.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have some legacy code that needs to identify in the Page_Load which event
I have some VBA code that needs to talk to a running c# application.
I have some code in a javascript file that needs to send queries back
we have some C++ code that we need to create a make file in.
I have some code that needs to know how many actual cores are available
I have some NDK based C++ code that needs to build an android bitmap
Follow up to a previous question , I have some code that needs to
I have a console application that require to use some code that need administrator
I have an HTML textbox that contains some SQL code that I need executed.
I have some code that gives a user id to a utility that then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.