Skip to main content
Minor improvements to naming and type
Source Link
Toby Speight
  • 88.3k
  • 14
  • 104
  • 327

This function is missing the necessary definitions for it to be usable. I suggest adding

#include <array>                // also defines std::size_t
#include <utility>

The function should have a comment summarising its purpose. It doesn't need to be very extensive.

Accepting argument using forwarding reference suggests we might move from it, but we never do (the std::array aggregate construction always copies), so it's better to accept as const-ref.

Type T needs to be copy-constructible, so we could specify that as a constraint, helping users by giving more meaningful errors.

Whilst N is easily understood as a size (and familiar to std::array users), x isn't very meaningful as argument name.

We can also use comma operator instead of defining deref to simplify converting the index sequence entries to array values:

#include <array>
#include <concepts>
#include <utility>

// Construct an array of size N, with every
// element initialised with value.
template<std::size_t N, std::copy_constructible T>
constexpr auto array_repeat(T const& value)
{
    return [&value]<auto... Indices>(std::index_sequence<Indices...>)
    {
        return std::array{ ((void)Indices,value)... };
    }(std::make_index_sequence<N>());
}

This function is missing the necessary definitions for it to be usable. I suggest adding

#include <array>                // also defines std::size_t
#include <utility>

The function should have a comment summarising its purpose. It doesn't need to be very extensive.

Accepting argument using forwarding reference suggests we might move from it, but we never do (the std::array aggregate construction always copies), so it's better to accept as const-ref.

We can also use comma operator instead of defining deref to simplify converting the index sequence entries to array values:

#include <array>
#include <concepts>
#include <utility>

// Construct an array of size N, with every
// element initialised with value.
template<std::size_t N, std::copy_constructible T>
constexpr auto array_repeat(T const& value)
{
    return [&value]<auto... Indices>(std::index_sequence<Indices...>)
    {
        return std::array{ ((void)Indices,value)... };
    }(std::make_index_sequence<N>());
}

This function is missing the necessary definitions for it to be usable. I suggest adding

#include <array>                // also defines std::size_t
#include <utility>

The function should have a comment summarising its purpose. It doesn't need to be very extensive.

Accepting argument using forwarding reference suggests we might move from it, but we never do (the std::array aggregate construction always copies), so it's better to accept as const-ref.

Type T needs to be copy-constructible, so we could specify that as a constraint, helping users by giving more meaningful errors.

Whilst N is easily understood as a size (and familiar to std::array users), x isn't very meaningful as argument name.

We can also use comma operator instead of defining deref to simplify converting the index sequence entries to array values:

#include <array>
#include <concepts>
#include <utility>

// Construct an array of size N, with every
// element initialised with value.
template<std::size_t N, std::copy_constructible T>
constexpr auto array_repeat(T const& value)
{
    return [&value]<auto... Indices>(std::index_sequence<Indices...>)
    {
        return std::array{ ((void)Indices,value)... };
    }(std::make_index_sequence<N>());
}
Minor improvements to naming and type
Source Link
Toby Speight
  • 88.3k
  • 14
  • 104
  • 327

This function is missing the necessary definitions for it to be usable. I suggest adding

#include <array>                // also defines std::size_t
#include <utility>

The function should have a comment summarising its purpose. It doesn't need to be very extensive.

Accepting argument using forwarding reference suggests we might move from it, but we never do (the std::array aggregate construction always copies), so it's better to accept as const-ref.

We can also use comma operator instead of defining deref to simplify converting the index sequence entries to array values:

#include <array>
#include <concepts>
#include <utility>

// Construct an array of size N, with every
// element initialised towith value x.
template<std::size_t N, std::copy_constructible T>
constexpr auto array_repeat(T const& xvalue)
{
    return [&x]<std::size_t[&value]<auto... Indices>(std::index_sequence<Indices...>)
    {
        return std::array{ ((void)Indices, xvalue)... };
    }(std::make_index_sequence<N>());
}

This function is missing the necessary definitions for it to be usable. I suggest adding

#include <array>                // also defines std::size_t
#include <utility>

The function should have a comment summarising its purpose. It doesn't need to be very extensive.

Accepting argument using forwarding reference suggests we might move from it, but we never do (the std::array aggregate construction always copies), so it's better to accept as const-ref.

We can also use comma operator instead of defining deref to simplify converting the index sequence entries to array values:

#include <array>
#include <concepts>
#include <utility>

// Construct an array of size N, with every
// element initialised to value x.
template<std::size_t N, std::copy_constructible T>
constexpr auto array_repeat(T const& x)
{
    return [&x]<std::size_t... Indices>(std::index_sequence<Indices...>)
    {
        return std::array{ ((void)Indices, x)... };
    }(std::make_index_sequence<N>());
}

This function is missing the necessary definitions for it to be usable. I suggest adding

#include <array>                // also defines std::size_t
#include <utility>

The function should have a comment summarising its purpose. It doesn't need to be very extensive.

Accepting argument using forwarding reference suggests we might move from it, but we never do (the std::array aggregate construction always copies), so it's better to accept as const-ref.

We can also use comma operator instead of defining deref to simplify converting the index sequence entries to array values:

#include <array>
#include <concepts>
#include <utility>

// Construct an array of size N, with every
// element initialised with value.
template<std::size_t N, std::copy_constructible T>
constexpr auto array_repeat(T const& value)
{
    return [&value]<auto... Indices>(std::index_sequence<Indices...>)
    {
        return std::array{ ((void)Indices,value)... };
    }(std::make_index_sequence<N>());
}
Source Link
Toby Speight
  • 88.3k
  • 14
  • 104
  • 327

This function is missing the necessary definitions for it to be usable. I suggest adding

#include <array>                // also defines std::size_t
#include <utility>

The function should have a comment summarising its purpose. It doesn't need to be very extensive.

Accepting argument using forwarding reference suggests we might move from it, but we never do (the std::array aggregate construction always copies), so it's better to accept as const-ref.

We can also use comma operator instead of defining deref to simplify converting the index sequence entries to array values:

#include <array>
#include <concepts>
#include <utility>

// Construct an array of size N, with every
// element initialised to value x.
template<std::size_t N, std::copy_constructible T>
constexpr auto array_repeat(T const& x)
{
    return [&x]<std::size_t... Indices>(std::index_sequence<Indices...>)
    {
        return std::array{ ((void)Indices, x)... };
    }(std::make_index_sequence<N>());
}