2

Suppose the following function

template<size_t N>
constexpr std::array<float, N> make_ones()
{
    std::array<float, N> ret{};
    for (size_t k = 0; k != N; ++k)
    {
        ret[k] = 1.0f;
    }
    return ret;
}

Is it possible to write that with a fold expression? The problem is that I do not have a pack to expand.

5
  • No. With recursive template -maybe../ but why not std::fill Commented Aug 4, 2023 at 18:54
  • This isn’t about fold expressions, just pack expansion (and the common problem of how to create a pack). Commented Aug 4, 2023 at 18:56
  • @Swift-FridayPie std::fill is only constexpr since C++20 Commented Aug 4, 2023 at 19:26
  • 1
    Why do you ask? This array initialization is evaluated once at compile time (and pretty fast). So what makes you think about fold expressions? (these are for parameter packs as you already note) Commented Aug 4, 2023 at 19:28
  • In C++20 or later, constexpr -> consteval to be absolutely certain that the array is filled at compile time (else compiler error will ensue). Commented Aug 4, 2023 at 21:46

1 Answer 1

4

Is it possible to write that with a fold expression?

Not with fold expression but pack expansion as well as with index sequence, in you could do:

template <size_t N>
constexpr std::array<float, N> make_ones()
{
    return []<size_t... Is>(std::index_sequence<Is...>) {
        return std::array<float, sizeof...(Is)>{( static_cast<void>(Is), 1.0f)...};
    }(std::make_index_sequence<N>{});
}

See live demo in godbolt.org


For compilers that doesn't support C++20 or later, you might do

template <size_t... Is>
constexpr std::array<float, sizeof...(Is)> make_ones_impl(std::index_sequence<Is...>)
{
    return std::array<float, sizeof...(Is)>{(static_cast<void>(Is), 1.0f)...};
}

template<size_t N>
constexpr std::array<float, N> make_ones()
{
    return make_ones_impl(std::make_index_sequence<N>{});
}
Sign up to request clarification or add additional context in comments.

5 Comments

Kudos, but, ehm, I know that readability is subjective, but I'd freak out if I had to review this. Do you consider the intent of this code explicit?
@MatG So would I.
@MatG It is common pattern for stuff like that. If you're unfamiliar with template metaprogramming you won't be able to read any generic code anyway(no matter how explicit it is). You can have a look at Haskel code, it is pure alien language until you learn it in details.
@MatG See the explanation here: gcc.godbolt.org/z/bGe3M7KG5
@MatG Would it help you to recognise the pattern, if made a bit more generic? I don't know how common it is in the wild, but you can find examples of its use in some Q&A here and other cpp resources available online.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.