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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:41:04+00:00 2026-05-27T23:41:04+00:00

In the code below, C’s base class B1’s template argument OFFSET depends on B0,

  • 0

In the code below, C’s base class B1’s template argument OFFSET depends on B0, and B2 on B1.

This is done by manual write the code every time an instance of C is created (in the main method). Is there a way to move this functionality to the definition of C instead?

template<int OFFSET>
struct A {
    enum O { offset = OFFSET };
    enum S { size = 2 };
};

template<int OFFSET>
struct B {
    enum O { offset = OFFSET };
    enum S { size = 4 };
};

template < typename B0, typename B1, typename B2 >
struct C : public B0, B1, B2 {
};

int main(int argc, const char *argv[])
{
    // instance of C
    C< A<1>,

       B< A<1>::offset * A<1>::size >,

       A<
           B< A<1>::offset * A<1>::size >::offset *
           B< A<1>::offset * A<1>::size >::size
       >
    > c1;

    // does the same thing
    C< A<1>,

       B< A<1>::size >,

       A<
           A<1>::size *
           B< A<1>::size >::size
       >
    > c2;

    return 0;
}

EDIT:

To answer the comments, here are the steps I think needs to be taken to solve this:

  • Write a metafunction which can change the offset:
    set_new_offset which for T defines the type T<2>

  • Use boost::mpl::times to calculate the new offsets

  • Add more template magic…

  • 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-27T23:41:05+00:00Added an answer on May 27, 2026 at 11:41 pm

    You can do it with template templates in C, although I’m not 100% sold it’s an improvement. If you only ever need three bases this should be fine. If you need an arbitrary number of bases…there must be a better way to do this than inheritance as this method will get unwieldy.

    template<int OFFSET>
    struct A {
        enum O { offset = OFFSET };
        enum S { size = 2 };
    };
    
    template<int OFFSET>
    struct B {
        enum O { offset = OFFSET };
        enum S { size = 4 };
    };
    
    template < typename B0, template <int T> class B1, template <int T> class B2 >
    struct C : public B0, B1<B0::offset * B0::size>, B2<B1<B0::offset * B0::size>::offset * B1<B0::offset * B0::size>::size> {
        enum
        {
            B0_offset = B0::offset,
            B1_offset = B1<B0::offset * B0::size>::offset,
            B2_offset = B2<B1<B0::offset * B0::size>::offset * B1<B0::offset * B0::size>::size>::offset,
            B0_size = B0::size,
            B1_size = B1<B0::offset * B0::size>::size,
            B2_size = B2<B1<B0::offset * B0::size>::offset * B1<B0::offset * B0::size>::size>::size
        };
    };
    
    int main()
    {
        // instance of C
        C< A<1>,
    
           B,
    
           A
        > c1;
    
        static_cast<void>(c1);
    
        // does the same thing
        C< A<1>,
    
           B,
    
           A
        > c2;
    
        static_cast<void>(c2);
    
        std::cout << c1.B0_offset << std::endl;
        std::cout << c1.B1_offset << std::endl;
        std::cout << c1.B2_offset << std::endl;
        std::cout << c1.B0_size << std::endl;
        std::cout << c1.B1_size << std::endl;
        std::cout << c1.B2_size << std::endl;
    
        std::cout << c2.B0_offset << std::endl;
        std::cout << c2.B1_offset << std::endl;
        std::cout << c2.B2_offset << std::endl;
        std::cout << c2.B0_size << std::endl;
        std::cout << c2.B1_size << std::endl;
        std::cout << c2.B2_size << std::endl;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code below is working well as long as I have class ClassSameAssembly in same
The code below gives me this mysterious error, and i cannot fathom it. I
Code below defines a ChargeCustomer class that contains an array of type customers. I
Code below is pseudo code. Imagine a class Fruit which has a factory method
code below. i'm tryind to obtain string answers like a1, c4 this is what
In below code i'm doing somthing wrong. Sorry if this is a bit basic.
This code below causes infinate loop problem (as documented). So how do I set
(code below regarding my question) Per this stack overflow question I used Pegolon's approach
Code Below: if(var != && var.startswith(somestring)) { do something } This code is troubling;
The code below works fine but, in the textbox the decimal value has this

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.