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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:02:16+00:00 2026-06-09T15:02:16+00:00

I am new to boost::mpl and TMP. I am trying to build a class

  • 0

I am new to boost::mpl and TMP. I am trying to build a class as follows –

template< typename T > 
class Demo
{
public:
    typedef boost::mpl::size<T> NumDimensions;

    template< size_t D >
    struct Dim
    {
        typedef typename boost::mpl::at_c< T, D >::type Type;
    };

    //I want to implement this function
    template< size_t D >
    typename Dim<D>::Type GetElement() 
    {
        if(D == 0)
        {
            return element1_;
        }
        if(D == 1)
        {
            return element2_;
        }
        ....
    }

private:
    typename Dim<0>::Type element1_;
    typename Dim<1>::Type element2_;
    ....
};

And I plan to use this class as follows –

typedef Demo< boost::mpl::vector< int, float, long > > D1;
D1 d;
D1::Dim<0>::Type i = d.GetElement<0>();

I am curious to know if there is any better way to declare these elements rather than hard-coding and repeating their declarations (and returns) – something (may be) like an array with heterogeneous types and simpler access to the elements.

Note:- This code may have compiler errors (I didn’t test it), but I hope it conveys my question.

  • 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-06-09T15:02:18+00:00Added an answer on June 9, 2026 at 3:02 pm

    You should find a way to create a tuple from your typelist.
    Here is an article that seems to describe how this could be done with C++11 featuers.
    http://www.devx.com/cplus/Article/41533/1954
    There was also a talk by Stephen Lavavej on “Going Native 2012”.

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

Sidebar

Related Questions

I'm very new to c++ and boost. I'm trying to get the host name
I'm new to the Boost.MPL library, and have some beginners-problems Look at this sample:
I am new to boost and metaprogramming so my problem is this: typedef fusion::vector<int,
I am new to boost. I am trying to implement boost::unorder_set. Here is the
I'm new to boost and I've been trying out boost::asio. The problem is I
I am new to boost python. I have to first init a cpp class
I have the following templated class structure struct TraitA{}; struct TraitB{}; template<typename trait> struct
I'm writing a template function which should swap two elements of a boost::mpl::vector (similarly
I'm trying to write a fairly simple threaded application, but am new to boost's
I am trying to pass a function pointer using boost::bind. void Class::ThreadFunction(Type(*callbackFunc)(message_type::ptr&)) { }

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.