Title
std::array member functions should be constexpr
Status
resolved
Section
[array]
Submitter
Peter Sommerlad

Created on 2014-10-06.00:00:00 last changed 100 months ago

Messages

Date: 2016-08-10.04:30:18

Proposed resolution:

This functionality is provided by P0031R0

Date: 2016-08-10.04:30:18

[ 08-2016, Post-Chicago ]

Move to Tentatively Resolved

Date: 2014-11-04.10:07:55

[ 2014-11 Urbana ]

Move to LEWG

The extent to which constexpr becomes a part of the Library design is a policy matter best handled initially by LEWG.

Date: 2014-10-06.00:00:00

When experimenting with C++14 relaxed constexpr functions I made the observation that I couldn't use std::array to create a table of data at compile time directly using loops in a function. However, a simple substitute I could use instead:

template <typename T, size_t n>
struct ar {
  T a[n];
  constexpr ar() : a{{}}{}
  constexpr auto data() const { return &a[0];}
  constexpr T const & operator[](size_t i) const { return a[i]; }
  constexpr T & operator[](size_t i) { return a[i]; }
};

template <size_t n>
using arr = ar<size_t, n>; // std::array<size_t, n>;

template <size_t n>
constexpr auto make_tab(){
  arr<n> result;
  for(size_t i=0; i < n; ++i)
    result[i] = (i+1)*(i+1); // cannot define operator[] for mutable array...
  return result;
}

template <size_t n>
constexpr auto squares=make_tab< n>();

int main() {
  int dummy[squares<5>[3]];
}

Therefore, I suggest that all member functions of std::array should be made constexpr to make the type usable in constexpr functions.

Wording should be straight forward, may be with the exception of fill, which would require fill_n to be constexpr as well.

History
Date User Action Args
2016-08-10 04:30:18adminsetmessages: + msg8468
2016-08-10 04:30:18adminsetmessages: + msg8467
2016-08-10 04:30:18adminsetstatus: lewg -> resolved
2014-11-04 10:07:55adminsetstatus: new -> lewg
2014-11-03 20:00:17adminsetmessages: + msg7163
2014-10-06 00:00:00admincreate