Thread: Can the new string literal prefix in C++11 be adaptive?

  1. #1
    Registered User
    Join Date
    Sep 2009

    Can the new string literal prefix in C++11 be adaptive?

    Hi there,

    C++11 introduces new string literal prefixes: u8, u, U.

    Sometimes my framework needs to change the default character type (by changing a typedef), for example in this program the type is wchar_t, then in another one it's char16_t. I find I have to change many code lines to give the string constants the correct prefix, otherwise it can not be compiled.

    When the default character type is set to wchar_t, all the string constants with prefix u will fail to compile, or when the type is char16_t the string constants with L prefix will fail.

    So my question is, can this prefix selection be adaptive? When the character type is set to char16_t I expect all the string constants automatically have the prefix u.

    Any suggestions from you? Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2006
    I'd say that you should do something like what windows does with the win32 api. define a macro whose definition changes based on the type of character being used. I believe windows uses T(), which is either no-effect when using char, but puts L in front of the string literals when using wide characters. so T("foo") translates to L"foo" after the preprocessor pass, when using unicode.

  3. #3
    Registered User
    Join Date
    Dec 2011
    I second Elkvis.

    Maybe like this:
    #define U8 or U16 or U32 as needed  
    #if defined U8 
    typedef char myChar 
    #define UX(x)    u8 ## x 
    #elif defined U16 
    typedef char16_t myChar 
    #define UX(x)    u ## x 
    #elif defined U32 
    typedef char32_t myChar 
    #define UX(x)    U ## x 
    typedef char myChar
    #define UX(x)    x 
    And then enclose all strings in UX() like:

    myChar * str = UX("My String");

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Close. MSVC uses _T(), you had it except for a missing underscore.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Sep 2009
    Thanks to you guys for the replies.

    Actually I was trying to find a way to do this with template other than macro, but after google searching seems my question finally turns out to be "re-instantiating a template every time it's invoked" which has no portable solution. So to my original question macro is a workable solution and simple enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modifying a string literal
    By Richardcavell in forum C Programming
    Replies: 3
    Last Post: 02-15-2011, 12:26 AM
  2. Modifying a string literal.
    By Eman in forum C++ Programming
    Replies: 45
    Last Post: 12-30-2010, 06:37 PM
  3. string literal assignment
    By @nthony in forum C Programming
    Replies: 1
    Last Post: 03-13-2009, 12:06 PM
  4. Replies: 7
    Last Post: 07-18-2005, 08:43 AM
  5. String literal
    By subdene in forum C++ Programming
    Replies: 5
    Last Post: 11-05-2002, 02:10 PM