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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:34:14+00:00 2026-06-07T23:34:14+00:00

I would like to initialize a constexpr array with a pattern that is generated

  • 0

I would like to initialize a constexpr array with a pattern that is generated using variadic template parameters. For simplicity, consider the problem of initializing a constexpr unsigned static array with the sizes of a list of types, say, unsigned, short, char, int, long. How can I do this so that all of the computation is done during compile time? I need the solution to play nice with the C++ type system, so I cannot use macros.

The best I could come up with is shown below, but compilation using g++ -std=c++11 -Wall -Ofast -S (using g++ 4.7) and inspection of the assembly clearly reveals that the values are pushed onto the stack during runtime. Any ideas? and works fine.

Using an array initializer as follows would work if I could somehow tell the expansion n +1 about expansion n.

static constexpr unsigned foo[] = { compute_element<Args>::value... };

Edit: Wait, never mind, I had a brainfart. The line above works fine…

Here is the code answer:

#include <iostream>

template <class... Args>
struct foo
{
    static constexpr unsigned bar[] = { sizeof(Args)... };
};

int main()
{
    std::cout << foo<unsigned, short, char, int, long>::bar[2] << std::endl;
    return 0;
}

Thank you very much for your time!

  • 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-07T23:34:16+00:00Added an answer on June 7, 2026 at 11:34 pm

    Here is the answer. Keep in mind that due to limitations in C++, I think that this can only be done in-compile time to create arrays that are of the same size of the variadic template parameter pack.

    #include <iostream>
    
    template <class... Args>
    struct foo
    {
        static constexpr unsigned bar[] = { sizeof(Args)... };
    };
    
    int main()
    {
        std::cout << foo<unsigned, short, char, int, long>::bar[2] << std::endl;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to initialize boost::random::discrete_distribution with an std::vector<double> . My problem is that
I would like to know: how can i initialize class object with parameters? I've
I would like to initialize a new string array from the values contained in
I have images in Images folder, and i would like to initialize an array
I am using Ruby on Rails 3 and I would like to initialize my
I would like to know how i can initialize an array(or list), yet to
I would like to initialize an array: $arr = array( date_edited => time(), date_added
I would like to initialize an array of structs, with the same element repetitively,
I would like to initialize 2 classes(say Class ARegister, Class BRegister) that registers some
I would like to initialize all the components of the game engine with the

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.