Macro, make wide string literal

Discussion in 'C++' started by Jim King, Apr 22, 2010.

  1. Jim King

    Jim King Guest

    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.
    Jim King, Apr 22, 2010
    #1
    1. Advertising

  2. Jim King

    Richard Guest

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

    Jim King <> spake the secret code
    <> thusly:

    >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.
    --
    "The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
    <http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/>

    Legalize Adulthood! <http://legalizeadulthood.wordpress.com>
    Richard, Apr 22, 2010
    #2
    1. Advertising

  3. Jim King

    Jim King Guest

    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?
    Jim King, Apr 22, 2010
    #3
  4. Jim King

    Jim King Guest

    On Apr 22, 9:15 pm, Victor Bazarov <> wrote:
    > Jim King wrote:
    > > 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?

    >
    > 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
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    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
    Jim King, Apr 22, 2010
    #4
  5. Jim King

    red floyd Guest

    On Apr 22, 6:39 am, Jim King <> wrote:
    > On Apr 22, 9:15 pm, Victor Bazarov <> wrote:
    >
    >
    >
    > > Jim King wrote:
    > > > 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?

    >
    > > 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
    > > --
    > > Please remove capital 'A's when replying by e-mail
    > > I do not respond to top-posted replies, please don't ask

    >
    > 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.
    red floyd, Apr 22, 2010
    #5
  6. Jim King

    Richard Guest

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

    red floyd <> spake the secret code
    <> thusly:

    >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__).
    --
    "The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
    <http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/>

    Legalize Adulthood! <http://legalizeadulthood.wordpress.com>
    Richard, Apr 22, 2010
    #6
  7. Jim King

    red floyd Guest

    On Apr 22, 11:43 am, Pete Becker <> wrote:
    > Richard wrote:
    > > [Please do not mail me a copy of your followup]

    >
    > > red floyd <> spake the secret code
    > > <> thusly:

    >
    > >> 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.

    >
    > 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. :)
    red floyd, Apr 22, 2010
    #7
  8. Jim King

    Jim King Guest

    Hi everyone,

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

    Regards,
    Jim King
    Jim King, Apr 23, 2010
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Web Developer

    char 8bit wide or 7bit wide in c++?

    Web Developer, Jul 31, 2003, in forum: C++
    Replies:
    2
    Views:
    570
    John Harrison
    Jul 31, 2003
  2. Disc Magnet
    Replies:
    2
    Views:
    695
    Jukka K. Korpela
    May 15, 2010
  3. Disc Magnet
    Replies:
    2
    Views:
    778
    Neredbojias
    May 14, 2010
  4. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    364
    Anonieko Ramos
    Sep 27, 2004
  5. Martin Rinehart

    80 columns wide? 132 columns wide?

    Martin Rinehart, Oct 31, 2008, in forum: Javascript
    Replies:
    16
    Views:
    160
    John W Kennedy
    Nov 13, 2008
Loading...

Share This Page