Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8627329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:15:09+00:00 2026-06-12T08:15:09+00:00

I want to create a unique variable name on the fly. This is my

  • 0

I want to create a unique variable name on the fly.

This is my code:

int call(int i)
{
    return i;
}
 
#define XCAT3(a, b, c)    a ## b ## c
 
#define CALL_2(arg, place, line) int XCAT3(cl, place, line) = call(arg);
 
#define CALL_1(arg)    CALL_2(arg, __FUNCTION__, __LINE__)
 
int main(int argc, char* argv[])
{
    CALL_1(1); /* this is line 20 */
    return 0;
}

This does work in GCC (http://ideone.com/p4BKQ) but unfortunately not in Visual Studio 2010 or 2012.

The error message is:

test.cpp(20): error C2143: syntax error : missing ‘;’ before ‘function_string’

test.cpp(20): error C2143: syntax error : missing ‘;’ before ‘constant’

test.cpp(20): error C2106: ‘=’ : left operand must be l-value

How can I create an on-the-fly unique variable name with C++?

Solution:

#define CAT2(a,b) a##b
#define CAT(a,b) CAT2(a,b)
#define UNIQUE_ID CAT(_uid_,__COUNTER__)

int main(int argc, char* argv[])
{
    int UNIQUE_ID = 1;
    int UNIQUE_ID = 2;
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:15:10+00:00Added an answer on June 12, 2026 at 8:15 am

    For a unique identifier, many implementations offer the __COUNTER__ preprocessor macro, that expands to an ever increasing number with every use.

    #define CAT(a,b) CAT2(a,b) // force expand
    #define CAT2(a,b) a##b // actually concatenate
    #define UNIQUE_ID() CAT(_uid_,__COUNTER__)
    
    auto UNIQUE_ID() = call(1); // may be _uid_0
    auto UNIQUE_ID() = call(2); // may be _uid_1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want create Dynamic Slideshow in Jquery i'm Write this code var ctx =
I'm using JPA 2.0 and want to create a unique constraint using XML, not
I want to create a less than or equal to 10 character unique string
I want to generate android device unique id for my android application to create
i have a unique challenge. i want to create a google analytics filter for
I'd like to create a unique temporary directory in Windows from some C code
I have this code in my ActionResult public ActionResult Copy( int bvVariableid ) {
i want create multiple search where statement $where_search is a multiple condition from post
I want create wordpress website into which I want create user management... That means
i want create a custom json data from the mssql 2008 results so that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.