Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no std::size?

Tags:

c++

stl

c++14

Scott Meyers, Herb Sutter and others are advocating non-member functions over member functions. And with additions such as std::begin, std::cend, etc, it seems the STL is moving in that direction. If that is the case, why is there no std::size?

I assume because it's easy enough to define your own version:

namespace std {
    // C++14
    template <typename C>
    decltype(auto) size(const C& c)
    {
        return distance(cbegin(c), cend(c));
    }
}

Or am I missing something? i.e. is there a reason not to use a non-member size?

like image 211
Daniel Avatar asked Jun 08 '26 23:06

Daniel


1 Answers

Why is there no std::size?

Because nobody wrote a proposal in time for it to be included in C++14. The first draft of the proposal to add it was dated May 2014, well after the C++14 standard was sent out for its last round of ballot in February 2014.

A revised version of the proposal was voted into the C++ working paper at the Committee's last meeting in November (see LWG motion 20), so it's likely that you'll see it in the next revision of the standard (C++17?), along with std::empty() and std::data().

like image 160
T.C. Avatar answered Jun 10 '26 12:06

T.C.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!