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 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

Related Questions

I know that one can define an 'expected' exception in JUnit, doing: @Test(expect=MyException.class) public
Many statically typed languages have parametric polymorphism. For example in C# one can define:
When, for example, making a colorscheme, how can one define #40ffff as UglyColor (i.e.
One can remove all calls to printf() using #define printf . What if I
How can I define one global GridViewColumnHeader.Click handler for any ListView.GridViewColumnHeader in my project?
How can I define and use two AverageTimer32 performance counters in one application (.exe)?
I would like to ask, how can I define class inside another one. In
When writing a header library (like Boost ), can one define free-floating (non-method) functions
Possible Duplicate: “static const” vs “#define” in c I am going through the K&R
User Defined Function (UDFs) are functions that one can program and can be dynamically

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.