create a unique temporary variable name via macro

  • Follow


{ Note: multi-posted to [clc++].  Authors: please state so in the 
article, so as not to needlessly use others' time. -mod }

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


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply travd (2) 8/14/2007 9:19:41 PM

In article <1187128494.083581.164560@l22g2000prc.googlegroups.com>,
<"travd@hotmail.com"> wrote:

> { Note: multi-posted to [clc++].  Authors: please state so in the 
> article, so as not to needlessly use others' time. -mod }
> 
> 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
  Why not do the simple thing and create a block containing the needed
region of code and a simple declaration.  Note

   int x[3];
   x[0] = 3;
   {
      int x = 4;
      std::cout << x << '\n';// prints 4;
   }
   std::cout <<  x[0]  << '\n'; // prints 3;
so what is wrong with this approach if you need lots of temps in a
region or they overlap then its time to factor the code.:)

  it makes more sense to a reader if even temps have readble names,

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Carl 8/14/2007 11:14:59 PM


On Aug 14, 10:14 pm, Carl Barron <cbarron...@adelphia.net> wrote:
> In article <1187128494.083581.164...@l22g2000prc.googlegroups.com>,
>
> <"tr...@hotmail.com"> wrote:
> > Hi,
>
> > I'm trying to use a macro to create a unique temporary variable name,
> >
> > [...]
> >
> > Any ideas?  Surely there must be a common idiom for this.
>
> > Travis

The 'trick' you are looking for uses the preprocessor to do token
pasting. You could generalize TEMP_OBJ a bit if necessary, but this
should get you started....

   // the code...
   #define _PASTE(a,b) a ## b
   #define PASTE(a,b) _PASTE(a, b)

   #define TEMP_OBJ(s) MyType PASTE(obj_, __LINE__)(s)

   struct MyType
   {
       MyType(const char*);
   };

   int main ()
   {
       TEMP_OBJ("aaa");
       TEMP_OBJ("bbb");
       return 0;
   }

   // the generated result
   int main ()
   {
       MyType obj_13("aaa");
       MyType obj_14("bbb");
       return 0;
   }

>
> Why not do the simple thing and create a block containing the needed
> region of code and a simple declaration.
>

Sometimes it is appropriate to use macros. Sometimes the name of the
variable is not important, so long as it doesn't collide with others
of its type or other locals. For instance, when an object constructor/
destructor have side effects that you want to happen every time a
scope is entered/exited [for execution tracing].

Travis


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Travis 8/15/2007 6:05:25 AM

travd@hotmail.com wrote:
>  I tried __LINE__, but I could
> find a way to paste it to obj_ to make the variable name.

You can do it, but then all uses of the variable have to be
on the same line, so I don't know how useful it's going to be.

#define TEMP_OBJ_HELPER(x,l) MyType obj_##l(x)
#define TEMP_OBJ(x) TEMP_OBJ_HELPER(x,__LINE__)

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Hyman 8/15/2007 9:47:21 AM

Hi,

If it is possible for you to go away from preprocessing, you could
consider using a class template with an integer parameter like this:

template<int i>
class Unique
{
  static int var;
}

template<int i>
int Unique<i>::var;

then you can use your new variables like this:

Unique<1234>::var = 1;
Unique<0>::var = 2;

and so on.

Greetings,
Olivier Langlois
http://www.olivierlanglois.net


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Olivier 8/17/2007 9:53:10 AM

4 Replies
761 Views

(page loaded in 0.233 seconds)

Similiar Articles:













7/20/2012 8:12:52 AM


Reply: