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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:04:41+00:00 2026-06-12T23:04:41+00:00

Possible Duplicate: Convert std::tuple to std::array C++11 Suppose you have: template<class T,int N> struct

  • 0

Possible Duplicate:
Convert std::tuple to std::array C++11

Suppose you have:

template<class T,int N>
struct A {
  A(const B& b): /* what comes here */ {}

  std::array<T,N> F;
};

I need each element of F[] to be constructed with the argument of the constructor, in above case b. This is tricky because the argument might not be of a type that can be a compile-time constant, like int etc.

This is different to Is it possible to construct the elements of a member array depending on an integral template parameter? since here a user-defined struct is used and thus we need run-time copies of it.

  • 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-12T23:04:43+00:00Added an answer on June 12, 2026 at 11:04 pm

    The indices trick can be applied here too, you just need to transform it a bit:

    template <std::size_t... Is>
    struct indices {};
    
    template <std::size_t N, std::size_t... Is>
    struct build_indices
      : build_indices<N-1, N-1, Is...> {};
    
    template <std::size_t... Is>
    struct build_indices<0, Is...> : indices<Is...> {};
    
    template<class T,int N>
    struct A {
      template<std::size_t... Is>
      A(const B& b, indices<Is...>) : F{{(void(Is),b)...}} {}
      A(const B& b) : A(b, build_indices<N>{}) {}
    
      std::array<T,N> F;
    };
    

    Live example.

    We basically ignore the values of the indices and only use the pack itself to perform the expansion, i.e. only the size of the pack is of interest to us. Reusing indices for this might seem like abuse, since we’re not interested in the actual values, but I think reusing the machinery here is fine. Any other construct to create an N-element pack would look the same, except that the pack would most likely just contain zeros.

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

Sidebar

Related Questions

Possible Duplicate: How to convert List<Integer> to int[] in Java? I have an ArrayList
Possible Duplicate: convert string to 2D array using php I have the string like
Possible Duplicate: Convert multidimensional array into single array I have this array: Array (
Possible Duplicate: Convert std::string to const char* or char* is there any to get
Possible Duplicate: Convert std::string to const char* or char* Simple question but I'm new
Possible Duplicate: convert string list to int list in haskell I have a string
Possible Duplicate: Convert std::string to const char* or char* void setVersion(char* buf, std::string version)
Possible Duplicate: Convert std::string to const char* or char* i've got a line from
Possible Duplicate: How to reuse an ostringstream? I have been using std::ostringstream to convert
Possible Duplicate: Convert a long hex string in to int array with sscanf I

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.