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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:25:59+00:00 2026-05-25T12:25:59+00:00

I have a set of classes that have the following structure: class U {

  • 0

I have a set of classes that have the following structure:

class U
{
public:
    explicit U(int) { ... }
    U() {...}
    Init(int) {...}
};

I need to be able to compose 1 or more of these classes into a class X. Pseudocode:

template<class TypeSequence>
class X that derives publicly from all the classes in TypeSequence
{
    X(int): all bases are initialized with the integer passed 
    {}
    //if the above constructor is impossible, then the following will do as well:
    X(int)
    {
        Call Init on all bases and pass the given int to them.
    }
};

I think I need a lot of mpl, but I’m not really good at it. Is what am I trying to do doable? A code sample would be great.

MY FAULT: Forgot to mention I can’t use C++11 features. I am looking for an MPL 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-25T12:26:00+00:00Added an answer on May 25, 2026 at 12:26 pm

    Well, Boost.MPL contains metafunctions inherit and inherit_linearly you can combine them with for_each to get the second variant (with init functions). Or using just boost::mpl::fold and custom metafunctions:

    struct Null_IntConstructor
    {
      Null_IntConstructor(int) { }
    };
    
    struct InheritFrom_IntConstructor_Folder
    {
      template<typename T1, typename T2>
      struct apply
      {
        struct type : T1, T2
        {
          type(int x) : T1(x), T2(x) { }
        };
      };
    };
    
    template<typename Bases>
    struct InheritFrom_IntConstructor 
      : boost::mpl::fold<Bases, 
                         Null_IntConstructor,
                         InheritFrom_IntConstructor_Folder>::type
    {
      InheritFrom_IntConstructor(int x) 
        : boost::mpl::fold<Bases, 
                           Null_IntConstructor,
                           InheritFrom_IntConstructor_Folder>::type(x) 
      { }
    };
    

    Usage example:

    #include <iostream>
    #include <boost/mpl/fold.hpp>
    #include <boost/mpl/vector.hpp>
    
    struct A
    {
      A(int x) { std::cout << "A::A " << x << std::endl; }
    };
    
    struct B
    {
      B(int x) { std::cout << "B::B " << x << std::endl; }
    };
    
    struct C
    {
      C(int x) { std::cout << "C::C " << x << std::endl; }
    };
    
    int main()
    {
      InheritFrom_IntConstructor< boost::mpl::vector<A, B, C> >(1);
    }
    

    Metafunction InheritFrom_IntConstructor can generalized to accept arbitrary type as constructor parameter and I’m not sure if can generalized to accept arbitrary number of arguments.

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

Sidebar

Related Questions

I have classes that are structured like the following: public class Forecast { [Key]
I have the following classes that represent tables: public class PriceDefinition { public virtual
I have the following classes that I need NHibernate to play nicely with. How
I have the following classes (trimmed to only show the basic structure): public abstract
If have a set of classes that all implement an interface. interface IMyinterface<T> {
I'm using ASP.NET MVC RC2. I have a set of classes that were auto-generated
I have a set of callback classes that I use for handling callbacks with
I have been assigned a project to develop a set of classes that act
I have a set of core, complicated JavaScript data structures/classes that I'd like to
I have written a set of classes and interfaces that are implemented in Moose

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.