C++ Logo


Advanced search

Re: std::array::size should be static

From: Michael Hava <mfh_at_[hidden]>
Date: Sun, 14 Jun 2020 20:40:42 +0000
Technically yes – maybe even Container::max_size in general should be static.

That’s an ABI break though and as there is a valid workaround (tuple_size) I’m not sure fixing this would have priority (let alone consensus) in LEWG…

@Jake: not sure what you mean with "or have the ability to have static methods that provide a static interface and a non-static interface." as we already have that, arr.size() would still work if size was static…

From: Std-Proposals <std-proposals-bounces_at_[hidden]ocpp.org> On Behalf Of Filippo Casarin via Std-Proposals
Sent: Saturday, June 13, 2020 12:54 AM
To: std-proposals_at_[hidden]
Cc: Filippo Casarin <casarin.filippo17_at_[hidden]>
Subject: [std-proposals] std::array::size should be static

With the current c++20 standard the following code

#include <array>
struct S {
    std::array<int, 4> arr;

    void foo() {

generate this error

main.cpp: In member function ‘void S::foo()’:
main.cpp:7:31: error: non-constant condition for static assertion
    7 | static_assert(arr.size());
      | ~~~~~~~~^~
main.cpp:7:31: error: use of ‘this’ in a constant expression

This would work if std::array::size would be static, also I don't think any old code would break for this change.

std::array::max_size and std::array::empty are similar and I think should be static.

Received on 2020-06-14 15:43:54