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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:14:51+00:00 2026-05-24T11:14:51+00:00

I am working on an application with a message based / asynchronous agent-like architecture.

  • 0

I am working on an application with a message based / asynchronous agent-like architecture.
There will be a few dozen distinct message types, each represented by C++ types.

class message_a
{
  long long identifier;
  double some_value;
  class something_else;
  ...//many more data members
}

Is it possible to write a macro/meta-program that would allow calculating the number of data members within the class at compile time?

//eg:

class message_b
{
  long long identifier;
  char foobar;
}


bitset<message_b::count_members> thebits;

I am not familiar with C++ meta programming, but could boost::mpl::vector allow me to accomplish this type of calculation?

  • 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-24T11:14:54+00:00Added an answer on May 24, 2026 at 11:14 am

    No, there is no way in C++ to know the names of all members or how many members are actually there.

    You could store all types in a mpl::vector along in your classes but then you face the problem of how to turn them into members with appropriate names (which you cannot achieve without some macro hackery).

    Using std::tuple instead of PODs is a solution that generally works but makes for incredible messy code when you actually work with the tuple (no named variables) unless you convert it at some point or have a wrapper that forwards accessors onto the tuple member.

    class message {
    public:
      // ctors
      const int& foo() const { return std::get<0>(data); }
      // continue boiler plate with const overloads etc
    
      static std::size_t nun_members() { return std::tuple_size<data>::value; }
    private:
      std::tuple<int, long long, foo> data;
    };
    

    A solution with Boost.PP and MPL:

    #include <boost/mpl/vector.hpp>
    #include <boost/mpl/at.hpp>
    #include <boost/preprocessor.hpp>
    #include <boost/preprocessor/arithmetic/inc.hpp>
    
    struct Foo {
      typedef boost::mpl::vector<int, double, long long> types;
    
    // corresponding type names here
    #define SEQ (foo)(bar)(baz)
    #define MACRO(r, data, i, elem) boost::mpl::at< types, boost::mpl::int_<i> >::type elem;
    BOOST_PP_SEQ_FOR_EACH_I(MACRO, 0, SEQ)
    
    };
    
    int main() {
      Foo a;
      a.foo;
    }
    

    I didn’t test it so there could be bugs.

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

Sidebar

Related Questions

I'm working on a web application. A user will create an email message that
I'm working on a Wicket-based web application. In the application there are buttons that
Hi I am working on a Vb.Net application . I have a compose message
I'm working on a TCP based application that processes bitpacked messages, meaning: The messages
I'm working on an application that simulates a Windows mouse based on other behavior.
I'm working on an application that contains several server sockets that each run in
Working with the Google Web Toolkit i have written an application which is based
I'm working on a browser-based application that needs to be able to get users'
I am working on a Tab Bar based application I have a class A
I was recently working on an application that sent and received messages over Ethernet

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.