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

The Archive Base Latest Questions

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

I have a lot of code like this: #define WITH_FEATURE_X struct A { #ifdef

  • 0

I have a lot of code like this:

#define WITH_FEATURE_X

struct A {
#ifdef WITH_FEATURE_X
  // ... declare some variables Y
#endif
  void f ();
};

void A::f () {
  // ... do something
#ifdef WITH_FEATURE_X
  // ... do something and use Y
#else
  // ... do something else
#endif
  // ... do something
}

and I’d like to replace the #defines with template parameters:

template < int WITH_FEATURE_X > // can be 0 or 1
struct A;

But I don’t want to duplicate almost the entire code of A::f() for A<0>::f() and A<1>::f() just for the few lines that depend on the parameter. I also don’t want to call functions instead of the previous #ifdefs. What is the common solution?

  • 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-16T17:53:15+00:00Added an answer on May 16, 2026 at 5:53 pm

    I believe what you want is an equivalent to the “static if” command that exists in D language. I am afraid such a feature does not exist in C++.

    Note that if parts of your code vary depending on the feature your request, these parts don’t belong in the main function because they are not part of the bare algorithm. So the option to delegate such features in functions seems like a good one.

    EDIT
    If your #ifdef statements are used to do the same subtask differently, then defining subfunctions is the right thing to do. It will make your code more readable, not less.

    If they are used for completely different actions, well, your code is already cluttered. Do something about it.

    As for the performance issue you fear might appear, trust your compiler.

    EDIT2
    I forgot to mention the answer to the first part of your code : use the following trick to add or remove members depending on “feature”.

    namespace helper
    {
      template<int feature>
      struct A;
    
      template<>
      struct A<0> { // add member variables for case 0 };
    
      template<>
      struct A<1> { // add member variables for case 1 };
    }
    
    template<int feature>
    class A : private helper::A<feature>
    {
      // ... functions here
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a lot of classes in the App_Code directory could this be a
I have a solution that is missing a lot of code coverage. I need
I have the following use case , lot of code which was tightly coupled
I have a lot of constants that are somehow related, at some point I
We have a lot of open discussions with potential clients, and they ask frequently
we have a lot of users running in different shared and solo-owned repositories in
I have a lot of spare intel linux servers laying around (hundreds) and want
I have a lot of buttons and by clicking on different button, different image
We have a lot of dll-libraries written in both delphi and c++ builder, and
I have spent lot of time doing research on VIM. I am Windows guy

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.