Document number: N4155

Date: 2014-09-28

Project: Programming Language C++, Library Working Group

Reply-to: Riccardo Marcangelo <ricky.65@outlook.com>

 

Non-member size() and more (Revision 1)

 

Introduction

This paper revises N4017 "Non-member size() and more" in response to feedback from the LEWG. Please see the original paper, N4017, for the rationale behind this proposal.

This paper includes the following changes from N4017 as requested by the LEWG:

ˇ         Removed front() and back().

ˇ         Added initializer_list support for the remaining functions (data() and empty()).

Note: The proposed wording uses C++14 auto return type deduction. This follows the precedent of the tuple apply() function in the Library Fundamentals TS which similarly uses decltype(auto).

Proposed Wording

Modify the section 24.3 Header <iterator> synopsis [iterator.synopsis] by adding the following at the end of the synopsis:

// 24.8, container access:

template <class C> constexpr auto size(const C& c) noexcept;

template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept;

template <class C> constexpr bool empty(const C& c) noexcept;

template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept;

template <class E> constexpr bool empty(initializer_list<E> il) noexcept;

template <class C> constexpr auto data(C& c) noexcept;

template <class C> constexpr auto data(const C& c) noexcept;

template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept;

template <class E> constexpr const E* data(initializer_list<E> il) noexcept;

Add a new section 24.8 Container access [iterator.container] containing the following:

In addition to being available via inclusion of the <iterator> header, the function templates in [iterator.container] are available when any of the following headers are included: <array>, <deque>, <forward_list>, <list>, <map>, <regex>, <set>, <string>, <unordered_map>, <unordered_set>, and <vector>.

template <class C> constexpr auto size(const C& c) noexcept;

            Returnsc.size().

template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept;

            Returns: N.

template <class C> constexpr bool empty(const C& c) noexcept;

            Returnsc.empty().

template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept;

            Returns: false.

template <class E> constexpr bool empty(initializer_list<E> il) noexcept;

            Effects: Equivalent to return il.size() == 0;

template <class C> constexpr auto data(C& c) noexcept;

template <class C> constexpr auto data(const C& c) noexcept;

            Returns: c.data().

template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept;

            Returns: array.

template <class E> constexpr const E* data(initializer_list<E> il) noexcept;

            Returns: il.begin().

Acknowledgments

A big thank you to Stephan T. Lavavej for his numerous and valuable feedback, suggestions, and corrections for this proposal.

Credit to Daniel Krügler for suggesting "container access" as the name of the new sub-clause in <iterator>.