Creating unique temporary variables using __LINE__ or other macro

Discussion in 'C++' started by travis.downs@gmail.com, Aug 14, 2007.

  1. Guest

    Hi,

    I'm trying to use a macro to create a unique temporary variable name,
    such as

    #define TEMP_OBJ(string) MyType obj_ <some magic here> (string);

    So something like

    TEMP_OBJ("foo")

    would evaluate to

    MyType obj_1234("foo");

    Where the 1234 is needed to make it unique. Thing is, I can't find a
    good way to make this unique stuff. I tried __LINE__, but I could
    find a way to paste it to obj_ to make the variable name.

    Any ideas? Surely there must be a common idiom for this.

    Travis
     
    , Aug 14, 2007
    #1
    1. Advertising

  2. wrote:
    > I'm trying to use a macro to create a unique temporary variable name,
    > such as
    >
    > #define TEMP_OBJ(string) MyType obj_ <some magic here> (string);


    Drop the semicolon after the macro definition.

    >
    > So something like
    >
    > TEMP_OBJ("foo")
    >
    > would evaluate to
    >
    > MyType obj_1234("foo");
    >
    > Where the 1234 is needed to make it unique. Thing is, I can't find a
    > good way to make this unique stuff. I tried __LINE__, but I could
    > find a way to paste it to obj_ to make the variable name.
    >
    > Any ideas? Surely there must be a common idiom for this.


    I used

    #define CONCAT(a, b) a ## b
    #define UNIQUENAME(prefix) CONCAT(prefix, __LINE__)

    Which then gets used in another macro

    #define PROFILER_ENTRY() \
    static MyProfiler::Entry * UNIQUENAME(ppe) = \
    MyProfiler::Instance().addEntry(... /* some other stuff */
    #define PROFILE_THIS PROFILER_ENTRY
    ...
    int foo() {
    PROFILE_THIS();

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 14, 2007
    #2
    1. Advertising

  3. Guest

    On Aug 14, 3:00 pm, "Victor Bazarov" <> wrote:
    > wrote:
    > > I'm trying to use a macro to create a unique temporary variable name,
    > > such as

    >
    > > #define TEMP_OBJ(string) MyType obj_ <some magic here> (string);

    >
    > Drop the semicolon after the macro definition.
    >
    >
    >
    > > So something like

    >
    > > TEMP_OBJ("foo")

    >
    > > would evaluate to

    >
    > > MyType obj_1234("foo");

    >
    > > Where the 1234 is needed to make it unique. Thing is, I can't find a
    > > good way to make this unique stuff. I tried __LINE__, but I could
    > > find a way to paste it to obj_ to make the variable name.

    >
    > > Any ideas? Surely there must be a common idiom for this.

    >
    > I used
    >
    > #define CONCAT(a, b) a ## b
    > #define UNIQUENAME(prefix) CONCAT(prefix, __LINE__)
    >
    > Which then gets used in another macro
    >
    > #define PROFILER_ENTRY() \
    > static MyProfiler::Entry * UNIQUENAME(ppe) = \
    > MyProfiler::Instance().addEntry(... /* some other stuff */
    > #define PROFILE_THIS PROFILER_ENTRY
    > ...
    > int foo() {
    > PROFILE_THIS();
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    Thanks very much for your response. I had already tried something
    very similar (although I had one less level of indirection - no
    PROFILE_THIS, just PROFILER_ENTRY). I tried your suggestion exactly
    __LINE__ does not get expanded for me, so I get an error about
    ppe__LINE__ being redeclared. I cannot make __LINE__ expand out
    before the concat, which is very frustrating.
     
    , Aug 15, 2007
    #3
  4. Bo Persson Guest

    wrote:
    :: On Aug 14, 3:00 pm, "Victor Bazarov" <>
    :: wrote:
    ::: wrote:
    :::: I'm trying to use a macro to create a unique temporary variable
    :::: name, such as
    :::
    :::: #define TEMP_OBJ(string) MyType obj_ <some magic here>
    :::: (string);
    :::
    ::: Drop the semicolon after the macro definition.
    :::
    :::
    :::
    :::: So something like
    :::
    :::: TEMP_OBJ("foo")
    :::
    :::: would evaluate to
    :::
    :::: MyType obj_1234("foo");
    :::
    :::: Where the 1234 is needed to make it unique. Thing is, I can't
    :::: find a good way to make this unique stuff. I tried __LINE__,
    :::: but I could find a way to paste it to obj_ to make the variable
    :::: name.
    :::
    :::: Any ideas? Surely there must be a common idiom for this.
    :::
    ::: I used
    :::
    ::: #define CONCAT(a, b) a ## b
    ::: #define UNIQUENAME(prefix) CONCAT(prefix, __LINE__)
    :::
    ::: Which then gets used in another macro
    :::
    ::: #define PROFILER_ENTRY() \
    ::: static MyProfiler::Entry * UNIQUENAME(ppe) = \
    ::: MyProfiler::Instance().addEntry(... /* some other
    ::: stuff */ #define PROFILE_THIS PROFILER_ENTRY
    ::: ...
    ::: int foo() {
    ::: PROFILE_THIS();
    :::
    ::: V
    ::: --
    ::: Please remove capital 'A's when replying by e-mail
    ::: I do not respond to top-posted replies, please don't ask
    ::
    :: Thanks very much for your response. I had already tried something
    :: very similar (although I had one less level of indirection - no
    :: PROFILE_THIS, just PROFILER_ENTRY). I tried your suggestion
    :: exactly __LINE__ does not get expanded for me, so I get an error
    :: about ppe__LINE__ being redeclared. I cannot make __LINE__ expand
    :: out before the concat, which is very frustrating.

    If you by any chance use MSVC with precompiled headers, this is a
    known bug there. The __LINE__ value is set when the header is
    (pre)compiled, not when your source file is compiled.

    The workaround offered is to use __COUNTER__ instead of __LINE__.


    Bo Persson
     
    Bo Persson, Aug 15, 2007
    #4
    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. Replies:
    2
    Views:
    495
    Raymond Martineau
    Feb 23, 2005
  2. Spry
    Replies:
    1
    Views:
    948
    Richard Heathfield
    Jul 26, 2003
  3. jake1138
    Replies:
    5
    Views:
    1,050
    Dave Thompson
    Feb 14, 2005
  4. Dom Gilligan

    Macro expansion of '#__LINE__'?

    Dom Gilligan, Nov 4, 2005, in forum: C Programming
    Replies:
    4
    Views:
    454
    Dom Gilligan
    Nov 4, 2005
  5. Replies:
    3
    Views:
    379
    Robert Gamble
    Feb 25, 2007
Loading...

Share This Page