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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:38:06+00:00 2026-06-13T01:38:06+00:00

My question refers to a question I’ve asked previously for a single-dimensional array: for

  • 0

My question refers to a question I’ve asked previously for a single-dimensional array:

for loop elimination

Can someone please help me extend the use of the indices trick to multiple dimensional arrays, such as in this example:

template<unsigned...> struct indices
{
};

template<unsigned M, unsigned... Is> struct indices_gen
  : indices_gen<M - 1, M - 1, Is...>
{
};

template<unsigned... Is> struct indices_gen<0, Is...> : indices<Is...>
{
};

template <typename T>
struct example
{
  template<typename ...U, typename
    = typename std::enable_if<all_of<std::is_same<U, T>...>::value>::type>
  example(U... args)
  {
    static_assert(3 * 2 == sizeof...(U),
      "wrong number of arguments in assignment");
    assign(indices_gen<M * N>(), args...);
  }

  template<size_type... Is, class... U>
  void assign(indices<Is...>, U... args)
  {
    [](...){}(((&array[0][0])[Is] = args)...);
  }

  T array[3][2];
};

int main()
{
  example<int> ex(1, 2, 3, 4, 5, 6);
  return 0;
}

Currently I depend on the requirement, that arrays are contiguous, but I’d like to assign array using pairs of indices, not just a single index (this way I’d be able to support types other than arrays, in particular, types that override operator[]). If I use 2 argument packs for assignment, I’ll assign only at indices (0, 0), (1, 1), …, also there is a small problem with argument packs not having the same lengths when dimensions of the array differ (as in the example).

  • 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-13T01:38:08+00:00Added an answer on June 13, 2026 at 1:38 am

    This can be made a lot easier by changing the code to access the array, not the indices generation. What you basically want is a 1D to 2D access mapping.

    Normally, people need it the other way around (2D to 1D), when they implement a 2D array in terms of a 1D array†:

    template<class T, unsigned M, unsigned N>
    struct md_array{
      T elems[M * N]; // same layout as 'T elems[M][N];'
    };
    

    The formula to get the 1D index i from the 2D index (x,y) is then i == x * N + y. We can explain this if we imagine our 1D elems from above as a 2D array (using M == 2 and N == 3):

     0, 1, 2, 3, 4, 5 // indices (i)
    [a, b, c, d, e, f]
    v // we want them as M (2) packs of N (3) elements
     0, 1, 2   0, 1, 2 // element indices (y)
    [a, b, c] [d, e, f]
    \___0___/ \___1___/ // pack indices (x)
    v // fusing the packs back together, we can see that we have
      // a constant offset for the packs, which is the N (3) times x
    0*3+0, 0*3+1, 0*3+2, 1*3+0, 1*3+1, 1*3+2
    [ a,     b,     c,     d,     e,     f ]
    

    Thus, we get i == x * N + y. We now need to solve this formula not for i but for x and y. For x, it’s rather easy (using math notation):

    i = x * N + y | -y
    i - y = x * N | *(1/N)
    i - y
    ----- = x
      N
    

    So x == (i - y) / N. Now, sadly, I don’t know how to solve this for y using pure math, but we don’t need that. Taking a look at the element indices, we can see that they wrap around N, and that can easily be done using the modulo operator. Thus, y == i % N.

    Now we can implement a method that takes a linear index i and returns the element at the solved (x, y) from that:

    template<unsigned I>
    T& get(){ constexpr auto y = I % 3; return array[(I-y)/3][y]; }
    

    and generalizing that:

    template<unsigned I>
    T& get(){ constexpr auto y = I % N, x = (I-y)/N; return array[x][y]; }
    

    Using constexpr to ensure that all the computation is done at compile-time.
    Now you can simply write assign as follows:

    template<unsigned... Is, class... U>
    void assign(indices<Is...>, U... args)
    {
      [](...){}((get<Is>() = args)...);
    }
    

    Q.E.D. (Live example.)


    † Now, you can make this easier on yourself by actually implementing the 2D array as a 1D array. 🙂 This way, you can straight-forwardly use (array[Is] = args)... and for the other cases use a simple access function that does the mapping:

    T& get(unsigned x, unsigned y){ return array[x * N + y]; }
    

    Live example.

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

Sidebar

Related Questions

i'm writing a data migration using south... but the question refers to the use
Note, this is not a duplicate of .prop() vs .attr() ; that question refers
Please refer my previous question for code sample Sockets: sometimes (rarely) packets are lost
Please refer to this background question. After constructing this COUNT, how would I then
Question from Object-Oriented JavaScript book: Imagine Array() doesn't exist and the array literal notation
This question refers to the question Show/hide fields depending on select value <select id=viewSelector>
Thanks for any thoughts. This question refers to an ASP.NET 4.0 web application. A
I have found some question which refers to my initial question : - on
I've asked this question recently a few different ways, but don't get an answer
Refers to my previous question : Show values in TDropDownList in PRADO. ok fine

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.