473,785 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating unique temporary variables using __LINE__ or other macro

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 '07 #1
3 14566
tr**********@gm ail.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(pref ix) CONCAT(prefix, __LINE__)

Which then gets used in another macro

#define PROFILER_ENTRY( ) \
static MyProfiler::Ent ry * UNIQUENAME(ppe) = \
MyProfiler::Ins tance().addEntr y(... /* 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
Aug 14 '07 #2
On Aug 14, 3:00 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
travis.do...@gm ail.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(pref ix) CONCAT(prefix, __LINE__)

Which then gets used in another macro

#define PROFILER_ENTRY( ) \
static MyProfiler::Ent ry * UNIQUENAME(ppe) = \
MyProfiler::Ins tance().addEntr y(... /* 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 '07 #3
tr**********@gm ail.com wrote:
:: On Aug 14, 3:00 pm, "Victor Bazarov" <v.Abaza...@com Acast.net>
:: wrote:
::: travis.do...@gm ail.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(pref ix) CONCAT(prefix, __LINE__)
:::
::: Which then gets used in another macro
:::
::: #define PROFILER_ENTRY( ) \
::: static MyProfiler::Ent ry * UNIQUENAME(ppe) = \
::: MyProfiler::Ins tance().addEntr y(... /* 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
Aug 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
8369
by: Dom Gilligan | last post by:
Is there any way to get the preprocessor to produce the current line number in double quotes? At first sight, gcc seems to replace __LINE__ last (which would make sense), and so won't replace it at all if it's preceded by '#'. Background: I want to produce a string giving the current file and line number in an array of structures, as follows: ---------------
1
6946
by: Spry | last post by:
Hi, I wanted to write macros for finding the number of memory allocations and deallocations also wanted to find the locations. The code I have is a pretty big one. I have a wrapper on top of malloc(int x) as Xmalloc(int x) Now I want to write a macro for Xmalloc which can log the location of the file and the line and the number of bytes allocated.
9
25262
by: qazmlp | last post by:
How exactly __FILE__ and __LINE__ macros are defined? Or, Is the definition of these macros implementation dependent ? I am wondering how easily they can get the file name and line number respectively.
3
5082
by: Leiji Liu | last post by:
Hi, I am curious if there are any pre-defined varables (constants?) in C? I saw some code with __LINE__, __FILE__, etc. Are those located in some include files? LL
9
2343
by: Francois Grieu | last post by:
Hello, I wrote this: #define M(x) enum { m##__LINE__ = x }; #line 1000 M(126) M(341) M(565) ...
5
579
by: Generic Usenet Account | last post by:
Can someone kindly explain why stringification of the compiler preprocessor macro __LINE__ requires two steps, instead of one? I wanted to pass the error location of a system call to perror() and I found that I had to use a kludgy way to stick in the line number. Thanks, Song ///// Code Snippet ///// #include <stdio.h>
3
7816
by: pistmaster | last post by:
Hi, I am trying the use the current line number in my logging and I want to stringify it so that I know the size of the buffer required to output the log string. I would have though I could use #__LINE__ in a macro like: #define LOG_ERROR(err) LogError(err, __FILE__, #__LINE__) but all i get in the output is #<linenumber>. Why should this be?
4
3182
by: Torsten Wiebesiek | last post by:
Hi, I have a problem with the preprocessor. I have written my own little assert macro. This is supposed to log a message (with log4cxx): #define LogAssert(Expression) \ if (Expression) { \ LOG4CXX_FATAL(log4cxx::Logger::getRootLogger(), "LogAssert: " \ #Expression " in file " __FILE__ ", line " #__LINE__ "."); \ ::exit(1); \
13
3715
by: mliptak | last post by:
I'm trying to implement logging in my application, so that each log message has its unique identifier, e.g. log(identifier, text) What I want to achieve is that the compiler screams if the log() with 'identifier' is also used in some other place in the code which would make the 'identifier' not unique. Is that something that can be achieved in C++? Thanks
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10341
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8979
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.