Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array Initialisation Compile Time - Constexpr Sequence

I was reading this question on SO.

The question itself is not so interesting, but I was wondering whether it exists and how to implement a compile time solution.

Regard to the first sequence:

All numbers except the ones which can be divided by 3.

The sequence should be something like:

[1, 2, 4, 5, 7, 8, 10, 11, 13, 14, ...]

By induction, I've found the math formula for that sequence:

 f(0) = 0;
 f(x > 0) = floor[(3x - 1) / 2];

So I've implemented a C++ constexpr function which generates the i-th number in the sequence:

#include <type_traits>

template <typename T = std::size_t>
constexpr T generate_ith_number(const std::size_t index) {
  static_assert(std::is_integral<T>::value, "T must to be an integral type");

  if (index == 0) return 0;
  return (3 * index - 1) / 2;
}

Now I'd like to generate a "compile-time array/sequence" which stores the first N-th numbers of the sequence.

The structure should be something like:

template <typename T, T... values>
struct sequence {};

template <typename T, std::size_t SIZE>
struct generate_sequence {};  // TODO: implement

Questions (more than one, but related among them):

1) How to implement that kind of integer_sequence?

2) Is it possible to build an std::array from that integer_sequence at compile time?

like image 968
BiagioF Avatar asked Aug 29 '17 13:08

BiagioF


People also ask

Does constexpr compile time?

constexpr functions Consuming code requires the return value at compile time to initialize a constexpr variable, or to provide a non-type template argument. When its arguments are constexpr values, a constexpr function produces a compile-time constant.

Is constexpr always evaluated at compile time?

constexpr functions will be evaluated at compile time when all its arguments are constant expressions and the result is used in a constant expression as well.


1 Answers

1) How to implement that kind of integer_sequence?

template <std::size_t... Is> 
constexpr auto make_sequence_impl(std::index_sequence<Is...>)
{
    return std::index_sequence<generate_ith_number(Is)...>{};
}

template <std::size_t N> 
constexpr auto make_sequence()
{
    return make_sequence_impl(std::make_index_sequence<N>{});
}

2) Is it possible to build an std::array from that integer_sequence at compile time?

template <std::size_t... Is>
constexpr auto make_array_from_sequence_impl(std::index_sequence<Is...>)
{
    return std::array<std::size_t, sizeof...(Is)>{Is...};
}

template <typename Seq>
constexpr auto make_array_from_sequence(Seq)
{
    return make_array_from_sequence_impl(Seq{});
}

Usage:

int main()
{
    constexpr auto arr = make_array_from_sequence(make_sequence<6>());
    static_assert(arr[0] == 0);
    static_assert(arr[1] == 1);
    static_assert(arr[2] == 2);
    static_assert(arr[3] == 4);
    static_assert(arr[4] == 5);
    static_assert(arr[5] == 7);
}

live example on wandbox.org

like image 118
Vittorio Romeo Avatar answered Sep 22 '22 02:09

Vittorio Romeo