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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:54:58+00:00 2026-05-17T19:54:58+00:00

consider the following templated datastructures enum eContent{ EINT = 1, EFLOAT = 2, EBOOL

  • 0

consider the following templated datastructures

enum eContent{
    EINT = 1,
    EFLOAT = 2,
    EBOOL = 4
};

template<int>
struct Container{
    Container(){assert(false);} //woops, don't do that!
};

template<>
struct Container<EINT>{
    Container():i(123){}
    int i;
};

template<>
struct Container<EFLOAT>{
    Container():f(123.456f){}
    float f;
};

template<>
struct Container<EBOOL>{
    Container():b(true){}
    bool b;
};



<fancy macro goes here that creates me all kind of combinations including for example>
    template<>
    struct Container<EFLOAT | EBOOL>: public Container<EFLOAT>, public Container<EBOOL>{
        Container():Container<EFLOAT>(),Container<EBOOL>(){}
    };
</fancy macro>

such that I then can for example define a variable like this:

Container<EINT|EFLOAT|EBOOL> myVar;

how would I define this fancy macro?

Why I want this?
Let it be for the sake of fun and learning metaprogramming

  • 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-17T19:54:58+00:00Added an answer on May 17, 2026 at 7:54 pm
    enum eContent{
        eInt    = 1,
        eFloat  = 2,
        eBool   = 4
    };
    
    template<unsigned, unsigned>
    struct Member {};
    
    template<>
    struct Member<eInt, eInt>{
        Member():i(123){}
        unsigned i;
    };
    
    template<>
    struct Member<eFloat, eFloat>{
        Member():f(123.456f){}
        float f;
    };
    
    template<>
    struct Member<eBool, eBool>{
        Member():b(true){}
        bool b;
    };
    
    template< unsigned members >
    struct Container
        : Member< members & eInt, eInt >
        , Member< members & eFloat, eFloat >
        , Member< members & eBool, eBool >
    {};
    
    int main()
    {
        Container< eFloat | eBool > c;
        c.f;    // OK
        c.b;    // OK
        c.i;    // !Nah
    }
    

    But I don’t think it’s good for anything, really, it’s just a solution to the literal problem you stated.

    If you have some real problem in mind (for which you think this could be a solution), try to ask about that.

    Unless it’s just play, or homework, of course. 🙂

    Cheers & hth.,

    PS: As a matter of good C++ programming practice, reserve ALL UPPERCASE names for macros, and only for macros. That way you avoid many potential name collisions. Using ALL UPPERCASE for constants is a Java/Python/etc. convention, to some degree suitable for those languages, but decidedly not for C++. It stems from early C, where constants had to be expressed as macros. ALL UPPERCASE was (and is) used for macros, not for constants — well, except Brian Kernighan, but let’s not delve into history… 😉

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

Sidebar

Related Questions

Consider the following code: template <int dim> struct vec { vec normalize(); }; template
Background Consider the following: template <unsigned N> struct Fibonacci { enum { value =
Consider the following code template<typename T, int N> struct A { typedef T value_type;
Consider the following class: class Foo { enum Flags {Bar, Baz, Bax}; template<Flags, class
Consider the following code template<unsigned int N> void foo(std::bitset<N> bs) { /* whatever */
Consider the following code: template<int* a> class base {}; int main() { base<(int*)0> test;
Consider the following code: template<class T, class F> struct X {}; template<class T, class
Consider the following program: template <typename T> struct t { struct base { void
Consider the following use of template template parameters... #include <iostream> template <typename X> class
Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T

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.