Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Creating unique temporary variables using __LINE__ or other macro

Reply
Thread Tools

Creating unique temporary variables using __LINE__ or other macro

 
 
travis.downs@gmail.com
Guest
Posts: n/a
 
      08-14-2007
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-14-2007
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


 
Reply With Quote
 
 
 
 
travis.downs@gmail.com
Guest
Posts: n/a
 
      08-15-2007
On Aug 14, 3:00 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> travis.do...@gmail.com 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.

 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      08-15-2007
wrote:
:: On Aug 14, 3:00 pm, "Victor Bazarov" <v.Abaza...@comAcast.net>
:: wrote:
::: travis.do...@gmail.com 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stringification of the __LINE__ macro pistmaster@googlemail.com C Programming 3 02-25-2007 08:05 PM
Macro expansion of '#__LINE__'? Dom Gilligan C Programming 4 11-04-2005 05:47 PM
Different behavior from __LINE__ macro in g++ 2.95.x and 3.x usenet@sta.samsung.com C++ 2 02-23-2005 09:22 PM
Macro Variable Argument List with __FILE__ and __LINE__ jake1138 C Programming 5 02-14-2005 09:46 AM
Macro for memory logging using __FILE__ and __LINE__ Spry C Programming 1 07-26-2003 01:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57