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

  • Home
  • SEARCH
  • 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 829395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:52:35+00:00 2026-05-15T03:52:35+00:00

One can define a static array at compile time as follows: const std::size_t size

  • 0

One can define a static array at compile time as follows:

const std::size_t size = 5;    
unsigned int list[size] = { 1, 2, 3, 4, 5 };

Question 1 – Is it possible by using various kinds of metaprogramming techniques to assign these values “programmatically” at compile time?

Question 2 – Assuming all the values in the array are to be the same barr a few, is it possible to selectively assign values at compile time in a programmatic manner?

eg:

const std::size_t size = 7;        
unsigned int list[size] = { 0, 0, 2, 3, 0, 0, 0 };
  1. Solutions using C++0x are welcome
  2. The array may be quite large, few
    hundred elements long
  3. The array for now will only consist of
    POD types
  4. It can also be assumed the size of
    the array will be known beforehand,
    in a static compile-time compliant
    manner.
  5. Solutions must be in C++ (no script, no macros, no pp
    or code generator based solutions pls)

UPDATE: Georg Fritzsche’s solution is amazing, needs a little work to get it compiling on msvc and intel compilers, but nonetheless a very interesting approach to the problem.

  • 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-05-15T03:52:36+00:00Added an answer on May 15, 2026 at 3:52 am

    The closest you can get is using C++0x features to initialize local or member arrays of templates from a variadic template argument list.
    This is of course limited by the maximum template instantiation depth and wether that actually makes a notable difference in your case would have to be measured.

    Example:

    template<unsigned... args> struct ArrayHolder {
        static const unsigned data[sizeof...(args)];
    };
    
    template<unsigned... args> 
    const unsigned ArrayHolder<args...>::data[sizeof...(args)] = { args... };
    
    template<size_t N, template<size_t> class F, unsigned... args> 
    struct generate_array_impl {
        typedef typename generate_array_impl<N-1, F, F<N>::value, args...>::result result;
    };
    
    template<template<size_t> class F, unsigned... args> 
    struct generate_array_impl<0, F, args...> {
        typedef ArrayHolder<F<0>::value, args...> result;
    };
    
    template<size_t N, template<size_t> class F> 
    struct generate_array {
        typedef typename generate_array_impl<N-1, F>::result result;
    };
    

    Usage for your 1..5 case:

    template<size_t index> struct MetaFunc { 
        enum { value = index + 1 }; 
    };
    
    void test() {
        const size_t count = 5;
        typedef generate_array<count, MetaFunc>::result A;
    
        for (size_t i=0; i<count; ++i) 
            std::cout << A::data[i] << "\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 436k
  • Answers 436k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Should work. Have you tried it? Any issues? After parsing… May 15, 2026 at 3:55 pm
  • Editorial Team
    Editorial Team added an answer Don't ignore exceptions, for exceptional and unexpected errors. However, just… May 15, 2026 at 3:55 pm
  • Editorial Team
    Editorial Team added an answer Check the documentation of array_merge: If, however, the arrays contain… May 15, 2026 at 3:55 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.