markt1964
markt1964

Reputation: 2826

How a C++11 constexpr constructor fully initialize a C-style array from a pointer?

In c++11, I want to have a struct like the following:

template<unsigned n> struct bytes_block {
    char data[n];
    constexpr bytes_block(char const *s):data(....) {}
};

It can be safely assumed that the constructor parameter 's' points to a region of memory where at least n consecutive characters can be copied from starting at 's' from without invoking any UB in the constructor.

I do not know how to fill in the ... above, however.

Can a constexpr implementation for the bytes_block constructor be made which would be C++11 compliant? Any number of additional constexpr functions may be created to be used as helpers, as long as of course they consist of only a single return statement.

Upvotes: 1

Views: 103

Answers (1)

Artyer
Artyer

Reputation: 40811

You can accomplish this by indexing with a parameter pack from std::make_index_sequence<n>. Of course, this doesn't exist in C++11, but it's easy enough to implement:

#include <cstddef>
#include <utility>

// Use `std::index_sequence` if available, otherwise implement it.
namespace detail {
#if __cplusplus < 201300L
    template<class T, T... Ints>
    struct integer_sequence {};

    template<std::size_t... Ints>
    using index_sequence = integer_sequence<std::size_t, Ints...>;

    template<typename Firsts, typename Last>
    struct index_sequence_eights_append;

    template<std::size_t... N, std::size_t... M>
    struct index_sequence_eights_append<index_sequence<N...>, index_sequence<M...>> {
        using type = index_sequence<
            N..., (sizeof...(N) + N)..., (2u * sizeof...(N) + N)..., (3u * sizeof...(N) + N)...,
            (4u * sizeof...(N) + N)..., (5u * sizeof...(N) + N)..., (6u * sizeof...(N) + N)...,
            (7u * sizeof...(N) + M)...
        >;
    };

    template<std::size_t N>
    struct make_index_sequence_helper {
        using type = typename index_sequence_eights_append<typename make_index_sequence_helper<N / 8u>::type, typename make_index_sequence_helper<N - 7u * (N / 8u)>::type>::type;
    };

    template<> struct make_index_sequence_helper<0> { using type = index_sequence<>; };
    template<> struct make_index_sequence_helper<1> { using type = index_sequence<0>; };
    template<> struct make_index_sequence_helper<2> { using type = index_sequence<0, 1>; };
    template<> struct make_index_sequence_helper<3> { using type = index_sequence<0, 1, 2>; };
    template<> struct make_index_sequence_helper<4> { using type = index_sequence<0, 1, 2, 3>; };
    template<> struct make_index_sequence_helper<5> { using type = index_sequence<0, 1, 2, 3, 4>; };
    template<> struct make_index_sequence_helper<6> { using type = index_sequence<0, 1, 2, 3, 4, 5>; };
    template<> struct make_index_sequence_helper<7> { using type = index_sequence<0, 1, 2, 3, 4, 5, 6>; };

    // Has a template instantiation depth of `4 + (log_2(N) / 3)`
    template<std::size_t N>
    using make_index_sequence = typename make_index_sequence_helper<N>::type;
#else
    using std::index_sequence;
    using std::make_index_sequence;
#endif
}

And then the constructor becomes as simple as delegating to get the pack and indexing with it:

template<unsigned n> struct bytes_block {
    char data[n];
    constexpr bytes_block(char const *s) : bytes_block(detail::make_index_sequence<n>{}, s) {}

private:
    template<std::size_t... I>
    constexpr bytes_block(detail::index_sequence<I...>, char const *s) : data{ s[I]... } {}
};

Upvotes: 1

Related Questions