Quantcast
Channel: Programmatically create static arrays at compile time in C++ - Stack Overflow
Browsing index pages (15 articles)

Programmatically create static arrays at compile time in C++

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

View Article


Answer by CB Bailey for Programmatically create static arrays at compile time...

Do you really need to do it at compiler time? It would be much easier to do at static initialization time. You could do something like this.#include <cstddef>#include...

View Article


Answer by valdo for Programmatically create static arrays at compile time in C++

There's a lot of things you can do with meta-programming.But first I'd like to ask: why would you want to do this in your case? I could understand if you needed to declare such an array in different...

View Article

Answer by danielkza for Programmatically create static arrays at compile time...

Something like Boost.Assignment could work for standard containers. If you really need to use arrays, you can use it along Boost.Array.

View Article

Answer by Michael Dorgan for Programmatically create static arrays at compile...

from boost, boost::mpl::range_c<int,1,5>Will generate a list of sorted numbers from 1 to 5 at compile time. For the second, you mention no criteria for which values would be changed. I'm pretty...

View Article


Answer by Max for Programmatically create static arrays at compile time in C++

the 1't question. You can do it like that.template <int num, int cur>struct ConsequentListInternal { enum {value = cur}; ConsequentListInternal<num-1,cur+1> next_elem;};template <int...

View Article

Answer by Michael Anderson for Programmatically create static arrays at...

How about building a nested struct using templates, and casting that as an array of the right type. The example below works for me, but I have a feeling I'm either treading in or walking very close to...

View Article

Answer by Matthieu M. for Programmatically create static arrays at compile...

Well your requirements are so vague it's difficult to do anything about them... The main issue is of course: where do those value come from ?Anyway a build in C++ can be thought of as 4 steps:Pre-build...

View Article


Answer by Georg Fritzsche for Programmatically create static arrays at...

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

View Article


Answer by Rui Curado for Programmatically create static arrays at compile...

Just use a code generator. Build one or more templates that can generate the code you want, using a table or even math functions. Then include the file you generated in your app.Seriously, a code...

View Article

Answer by kwesolowski for Programmatically create static arrays at compile...

Sometime (not always) such array is generated from array of types.For example if you already have variadic class list (like template) and want to store encapsulated uint32_t value you can use:uint32_t...

View Article

Answer by zinx chung for Programmatically create static arrays at compile...

use template recursivetemplate<uint64_t N>constexpr uint64_t Value(){ return N + 100;}// recursive casetemplate<uint64_t N, uint64_t... args>struct Array : Array<N - 1, Value<N -...

View Article

Answer by janekb04 for Programmatically create static arrays at compile time...

Since C++17 you can use a constexpr lambda an invoke it in place. The only "downside" is that you will have to use std::array instead of c-style array:constexpr auto myArray{[]() constexpr{...

View Article


Answer by Antonin GAVREL for Programmatically create static arrays at compile...

array<int, SIZE> tAs mentionned, with C++17 you can use constexprvector<int> countBits(int num) { static constexpr int SIZE = 100000; static constexpr array<int, SIZE> t {[]()...

View Article

Answer by Kai Petzke for Programmatically create static arrays at compile...

Over time, the capabilities of constexpr functions, methods and lambdas have greatly improved in C++. With C++17, you may use for loops and if conditions to actually calculate the contents of an...

View Article

Browsing index pages (15 articles)


Latest Images