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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:09:26+00:00 2026-05-16T02:09:26+00:00

I’ve some code that produces a set of tr1::array of different sizes, but same

  • 0

I’ve some code that produces a set of tr1::array of different sizes, but same type, like

array<int, 2>
array<int, 4>
array<int, 6>

The number of these arrays, and their sizes, are given in compile time, so I know exactly how many of them there will be and how’s big each one (but they may be different).

Problem: I would like to put them in a collection (using array<> would be great), but type must be equal for all the members and this is not the case.

I thought about using boost::variant, but how can specify a variant with a compile-time determined list of types (I’m thinking about an heavy usage of the preprocessor…)?
What about using boost::any? Other methods? (Wild pointers?)

TIA
~Aki

Correction: preprocessor is not usable in this case.

  • 1 1 Answer
  • 1 View
  • 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-16T02:09:26+00:00Added an answer on May 16, 2026 at 2:09 am

    I would use Boost’s MPL and Fusion libraries. There are two ways of ending up with the type list: generate them, or explicitly define them. The former is bit more flexible, but it’s hard to say which is right for you since we don’t know how you get the values you have.

    In any case, generating:

    #include <boost/mpl/for_each.hpp>
    #include <boost/mpl/range_c.hpp>
    #include <boost/mpl/transform.hpp>
    #include <boost/mpl/vector.hpp>
    #include <array>
    #include <iostream>
    
    namespace bmpl = boost::mpl;
    
    // turns an index into an array
    template <typename T>
    struct make_array
    {
        // or whatever scheme you have
        static const std::size_t size = T::value * 2;
    
        // define generated type
        typedef std::array<int, size> type;
    };
    
    // list of values to convert
    typedef bmpl::range_c<size_t, 1, 10> array_range;
    
    // transform that list into arrays, into a vector
    typedef bmpl::transform<array_range, make_array<bmpl::_1>,
                                bmpl::back_inserter<bmpl::vector<>>
                                    >::type array_collection;
    

    Or explicitly stating:

    #include <boost/mpl/vector.hpp>
    #include <array>
    #include <iostream>
    
    namespace bmpl = boost::mpl;
    
    // list all array types
    typedef bmpl::vector<
                std::array<int, 2>,
                std::array<int, 4>,
                std::array<int, 6>,
                std::array<int, 8>,
                std::array<int, 10>,
                std::array<int, 12>,
                std::array<int, 14>,
                std::array<int, 16>,
                std::array<int, 18>
                    > array_collection;
    

    Either way, you can then use it like this:

    #include <boost/fusion/algorithm.hpp>
    #include <boost/fusion/container/vector.hpp>
    #include <boost/fusion/mpl.hpp>
    #include <boost/fusion/sequence.hpp>
    #include <boost/mpl/for_each.hpp>
    #include <typeinfo>
    
    // fusion "fuses" the bridge between MPL and runtime
    namespace bf = boost::fusion;
    
    struct print_type
    {
        template <typename T>
        void operator()(const T&) const
        {
            std::cout << typeid(T).name() << "\n";
        }
    };
    
    struct print_values
    {
        template <typename T>
        void operator()(const T& pArray) const
        {
            std::cout << "Printing array with size "
                        << pArray.size() << ":\n";
            std::for_each(pArray.begin(), pArray.end(),
                    [](int pX)
                    {
                        std::cout << pX <<  " ";
                    });
            std::cout << std::endl;
        }
    };
    
    int main(void)
    {
        // print all the types you have
        bmpl::for_each<array_collection>(print_type());
        std::cout.flush();
    
        // make a usable type out of the typelist
        typedef bf::result_of::as_vector<array_collection>::type array_fusion;
        array_fusion arrays; // now have an array of different arrays,
                             // compile-time generated but run-time usable
    
        // access like this:
        bf::at_c<0>(arrays)[1] = 5; 
        bf::at_c<1>(arrays)[2] = 7; 
        bf::at_c<2>(arrays)[0] = 135; 
    
        // for_each:
        bf::for_each(arrays, print_values());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.