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

  • Home
  • SEARCH
  • 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 7571899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:47:07+00:00 2026-05-30T15:47:07+00:00

This is originally posted as an answer to c++ macros with memory? But somehow

  • 0

This is originally posted as an answer to c++ macros with memory?

But somehow I am not able to get this compiled. I might be missing something here. (I have a feeling that this is something C++ can do)

main.cpp

#include <iostream>
using namespace std;

const char * hello = "hello";
const char * world = "world";

#define VAR

#define MEMORIZE world
#include "memorize.h"
#define MEMORIZE hello
#include "memorize.h"

int main() {
    cout << VAR << endl;
    return 0;
}

memorize.h

#undef VAR
#ifndef MEMORIZE
    # error "No Argument to memorize.h"
#endif
#define VAR MEMORIZE
#undef MEMORIZE

The compile error that I am getting is this:

[100%] Building CXX object CMakeFiles/main.dir/main.cpp.o
error: use of undeclared identifier 'MEMORIZE'
    cout << VAR << endl;
            ^
note: instantiated from:
#define VAR MEMORIZE
            ^
1 error generated.
make[2]: *** [CMakeFiles/main.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2

I really want to get this memory thing to work in preprocessing phase. Can someone help? I think BOOST_PP_COUNTER in 1.49 also uses this technique but i couldn’t figure out how.

  • 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-05-30T15:47:09+00:00Added an answer on May 30, 2026 at 3:47 pm

    You are using only a single VAR value (the last one) because it can take only one value. If you want to mean different things by VAR depending on the context, you need to have source statements after each include.

    #define xstr(a) str(a)
    #define str(a) #a
    int main() {
    #define MEMORIZE world
    #include "memorize.h"
          cout << VAR << endl;
    #undef MEMORIZE
    #define MEMORIZE hello
    #include "memorize.h"
          cout << VAR << endl;
              return 0;
    }
    

    memorize.h:

    #undef VAR
    #ifndef MEMORIZE
        # error "No Argument to memorize.h"
    #endif
    #define VAR xstr(MEMORIZE)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Originally posted on Server Fault , where it was suggested this question might better
This originally was a problem I ran into at work, but is now something
[originally posted on Symfony forum but no (proper) answer has been given yet]. Has
I originally posted this question looking for an answer with using python, got some
Note: This question has been originally posted by Lahiru Gunathilake as an answer to
Note this question was originally posted in 2009, before C++11 was ratified and before
Update I don't think I was clear enough when I originally posted this quesion.
Note: Originally this question was asked for PostgreSQL, however, the answer applies to almost
I originally asked this question on RefactorMyCode , but got no responses there... Basically
I originally posted this question on the miglayout forum and after 534 views and

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.