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 1089323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:12:15+00:00 2026-05-16T23:12:15+00:00

I want to write preprocessor functions/arrays that are evaluated at compile time. For example,

  • 0

I want to write preprocessor functions/arrays that are evaluated at
compile time. For example, if I define

#define MYARR[] {5,4,3,2,1,0}

then, the code

int x = R[0];

should be presented as

int x = 5;

to the compiler. (Of course only literals can be used in the index).
This is important if code size/memory is critical and we don’t want to
store MYARR, but we need it for coding convenience.

Compile time functions would also be good. For example, something like

#define MYMAP(n)
#if n==1
5
#else
2

So, the statement

int x = MYMAP(4);

should be presented to the compiler as

int x = 2;

Obviously, we have to use a literal as the argument. Is this possible?

  • 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-16T23:12:15+00:00Added an answer on May 16, 2026 at 11:12 pm

    Sure it’s possible. While you could do that manually, Boost.Preprocessor already gives you the tools you need:

    #include <boost/preprocessor.hpp>
    #define SEQ (5)(4)(3)(2)(1)(0)
    int x = BOOST_PP_SEQ_ELEM(0, SEQ);
    

    … gets transformed to:

    int x = 5;
    

    It also includes arithmetic, comparisons and control structures like IF, FOR, FOR_EACH, enumerations, … You just have to keep in mind that the data types you can work with are rather limited.

    Utilizing Boost.PP again, your second example can be done like so:

    #define MYMAP(x) BOOST_PP_IF(BOOST_PP_EQUAL(x, 1), 5, 2)
    

    You can of course implement manually what Boost.PP does, but considering the time and effort needed for this I personally wouldn’t bother.

    As a C user, you won’t be interested in the rest of Boost, so you might want to use bcp to extract the preprocessor components.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want write jquery code that will show form inputs after a checkbox is
Is it possible in C# to write MSIL code that will add a preprocessor
I want to write some C code that has the following requirement: Repeated fork
I want write a little code analyzer which parses nested structures and translates into
I want to write a program in Prolog that confirms if a b-tree of
I want to write a class with three int values in them and manipulate
I want to write a subroutine for working out what to do and then
I'm looking to write a Python import filter or preprocessor for source files that
I have whole page HTML in my database and i want write that html
I want write code once and use in different activities. I have created a

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.