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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:31:58+00:00 2026-05-20T18:31:58+00:00

I’m trying to use boost::mpl::inherit_linearly to compose a container class using types provided by

  • 0

I’m trying to use boost::mpl::inherit_linearly to compose a container class using types provided by the user:

#include <typeinfo>
#include <iostream>
#include <vector>

#include <boost/mpl/inherit.hpp>
#include <boost/mpl/inherit_linearly.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/vector.hpp>

namespace mpl = ::boost::mpl;

//////////////////////////////////////////////
// Create the container by chaining vectors
//////////////////////////////////////////////

struct Base {};

// Types provided by the user
typedef mpl::vector<int, char, double>::type myTypes;

typedef mpl::inherit_linearly<
    myTypes, 
    mpl::inherit<mpl::_1, std::vector<mpl::_2> >,
    Base
    >::type InheritedContainer;


// Function for accessing containers
template <typename T>
inline std::vector<T>& get_container(Base& c) {
    return static_cast<std::vector<T>& >(c);
}


// Some functions that manipulate the containers
//     NB: These functions only know about the Base and the types
//         they want to access

void my_int_func(Base& b) {
    get_container<int>(b).push_back(42);
}

void my_char_func(Base& b) {
    get_container<char>(b).push_back('c');
}

int main() {
    InheritedContainer container;
    Base& bref = container;

    my_int_func(bref);
    std::cout << "Int: " << get_container<int>(bref).back() << std::endl;

    my_char_func(bref);
    std::cout << "Char: " << get_container<char>(bref).back() << std::endl;

    return 0;
}

The compile error I get is:

question.cpp: In function ‘std::vector<T, std::allocator<_CharT> >& get_container(Base&) [with T = int]’:
question.cpp:40:   instantiated from here
question.cpp:31: error: invalid static_cast from type ‘Base’ to type ‘std::vector<int, std::allocator<int> >&’
question.cpp: In function ‘std::vector<T, std::allocator<_CharT> >& get_container(Base&) [with T = char]’:
question.cpp:44:   instantiated from here
question.cpp:31: error: invalid static_cast from type ‘Base’ to type ‘std::vector<char, std::allocator<char> >&’

Shouldn’t Base be a base of whatever type is produced by inherit_linearly? And if so, shouldn’t a vector<int> and the other vectors show up in the type hierarchy for static_cast to pull out?

Is there any other way to get this functionality?

  • 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-20T18:31:59+00:00Added an answer on May 20, 2026 at 6:31 pm

    I think Base is a base class of InheritedContainer, but not of
    std::vector<int>.
    As we know, std::vector isn’t defined as the following:

    class vector : Base {...
    

    You may expect the following inheritance:

    class InheritedContainer : Base, std::vector<int>, ... {...
    

    However, in this case, the cast from Base to vector<int> is a cross-cast,
    so this cannot be done with static_cast.

    As you may know, the following is allowed:

    InheritedContainer container;
    Base& bref = container;
    InheritedContainer& iref = static_cast<InheritedContainer&>(bref);
    std::vector<int>& vi = iref;
    std::vector<char>& vc = iref;
    

    If you can prepare get_container, my_int_func and my_char_func, probably the types
    to which the std::vector will be specialized are known beforehand.
    If so, I suppose it is pertinent to hold InheritedContainer& instead of Base&
    all along.
    If you have to cast Base to vector<T>, probably
    RTTI(for example adding virtual function to Base) and dynamic_cast will enable
    the cast.

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

Sidebar

Related Questions

No related questions found

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.