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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:26:15+00:00 2026-06-10T22:26:15+00:00

I have the following code inside a C++ class: class Features { #define Feature_Size_A

  • 0

I have the following code inside a C++ class:

class Features
{
    #define Feature_Size_A 12345
    #define Feature_Size_B 45678
    #define Feature_Size_C 78901
    //#define Feature_Size_D 14725

    const int Feature_Sum = 0
    #ifdef Feature_Size_A
        + Feature_Size_A
    #endif
    #ifdef Feature_Size_B
        + Feature_Size_B
    #endif
    #ifdef Feature_Size_C
        + Feature_Size_C
    #endif
    #ifdef Feature_Size_D
        + Feature_Size_D
    #endif
        ;

    #ifdef Feature_Size_A
        static float Feature_A[Feature_Size_A];
    #endif
    #ifdef Feature_Size_B
        static float Feature_B[Feature_Size_B];
    #endif
    #ifdef Feature_Size_C
        static float Feature_C[Feature_Size_C];
    #endif
    #ifdef Feature_Size_D
        static float Feature_D[Feature_Size_D];
    #endif
};

I used to comment out features, like line 4, to compile and run different tests. But now I’d like to have the class as a template, so I can instantiate several versions with different features turned on or off in the same program.

I’m thinking of something like this:

template <bool Feature_A, bool Feature_B, bool Feature_C, bool Feature_D>
class Features
{
    ...
};

Features<true, true, true, false> f;

I tried with boost::mpl:vector’s but I’m struggling harshly.

BTW: This is not the complete code. The original code has 25 features.

I’m thankful for every idea not involving macros 🙂

  • 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-10T22:26:16+00:00Added an answer on June 10, 2026 at 10:26 pm

    Type lists can be used to solve this problem.

    template<unsigned num, unsigned size, typename T>
    class Feature : public T
    {
    public:
        static float feature[size];
        static const unsigned int feature_sum = size + T::feature_sum;
    };
    template<unsigned num, unsigned size, typename T>
    float Feature<num, size, T>::feature[size];
    class Tail { 
    public:
        static const unsigned feature_sum = 0; 
    };
    
    template<unsigned num, unsigned size, typename T>
    float* get_feature_arr(Feature<num, size, T>& ref) 
    {
        return ref.feature;
    }
    
    int main() {
        Feature<1, 12345, Feature<2, 45678, Feature<4, 78901, Tail>>> TripleFeatures;
        auto first = get_feature_arr<1>(TripleFeatures);
        auto third = get_feature_arr<4>(TripleFeatures);
        auto size = TripleFeatures.feature_sum;
    }
    

    This can also be used to access any feature, regardless of what other features are or aren’t in the list.

    Edit: I nubbed up some of the details, like not defining the array and trying to have “3features” as an identifier. Le fixed. Code compiles GCC 4.7.1.

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

Sidebar

Related Questions

I have the following code inside a class: this.buttons = { cancelButton:{buttonId: cancelButton, buttonText:Cancel,
I have the following code inside a static method in a static class: Random
I have the following code inside a class. (It's coffeescript-- and it's for a
Inside a class, I have the following piece of code: /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Errors, typeof(ErrorsType))]
Inside a class of mine I have the following code: mHandler = createHandler(); private
I have the following code inside the NSData+AES256 class. I am trying AES CBC
I have the following code inside my @interface FriendsNavController : UINavigationController class implementation. The
I have the following code inside a loop: Range(N & i + 1).Formula =
I have the following code with a bool if statement inside and in another
Inside my template function I have the following code: TypeName myFunction() { TypeName result;

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.