constexpr for std::array

259 views
Skip to first unread message

Ed Rosten

unread,
May 13, 2016, 8:05:29 AM5/13/16
to ISO C++ Standard - Discussion

I'm planning on writing a proposal to make all relevant methods of std::array constexpr. This will make the std::array type useful for compile time manipulation of arrays. My current motivation for this is to make it easy to compute lookup tables, a minimal example being:


#include <cstddef>
#include <cmath>

template<class T, int N>
struct array
{
    T elems[N];

    constexpr T& operator[](size_t i)
    {
        return elems[i];
    }

    constexpr const T& operator[](size_t i) const
    {
        return elems[i];
    }

    constexpr size_t size() const
    {
        return N;
    }
};

//Function to build the lookup table
constexpr array<float, 10> make_lut()
{
    array<float, 10> a = {};

    for(size_t i=0; i < a.size(); i++)
        a[i] = std::exp(i);

    return a;
}

constexpr static auto lut = make_lut();


Is this as straightforward as it looks, or is there anything I've overlooked?

Regards

-Ed


Daniel Krügler

unread,
May 13, 2016, 8:12:22 AM5/13/16
to std-dis...@isocpp.org
2016-05-13 14:05 GMT+02:00 Ed Rosten <e...@edwardrosten.com>:
>
> I'm planning on writing a proposal to make all relevant methods of
> std::array constexpr. This will make the std::array type useful for compile
> time manipulation of arrays. My current motivation for this is to make it
> easy to compute lookup tables, a minimal example being:

Which working draft are you looking at? std::array is now fully
constexpr except for

void fill(const T& u);
void swap(array&) noexcept(is_nothrow_swappable_v<T>);

after acceptance of

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0107r0.pdf

I would say, that your proposal is not needed any more.

- Daniel

Edward Rosten

unread,
May 13, 2016, 8:17:46 AM5/13/16
to ISO C++ Standard - Discussion


On Friday, 13 May 2016 13:12:22 UTC+1, Daniel Krügler wrote:
 
Which working draft are you looking at?

Looks like I managed to get hold of an old one by mistake!


> I would say, that your proposal is not needed any more.

I agree.

Regards,

-Ed



 
std::array is now fully
constexpr except for

void fill(const T& u);
void swap(array&) noexcept(is_nothrow_swappable_v<T>);

after acceptance of

>
> I'm planning on writing a proposal to make all relevant methods of
> std::array constexpr. This will make the std::array type useful for compile
> time manipulation of arrays. My current motivation for this is to make it
> easy to compute lookup tables, a minimal example being: s/2015/p0107r0.pdf

- Daniel
Reply all
Reply to author
Forward
0 new messages