C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Redundant std::begin() and std::end() overloads for std::initializer_list

From: Jiang An <de34_at_[hidden]>
Date: Tue, 30 Aug 2022 22:07:46 +0800
You shouldn't write something like this, since the returned iterator (pointer) is very likely immediately dangling.

Yours,
Jiang An



发自我的小米
在 Nikl Kelbon via Std-Proposals <std-proposals_at_[hidden]>,2022年8月30日 22:05写道:

its not useless in case
begin({1,2,3})

вт, 30 авг. 2022 г. в 18:43, hamburger blacktea via Std-Proposals <std-proposals_at_[hidden]>:
See https://stackoverflow.com/q/25215590. That's an old question (in 2014).

So I want to solve it:

Proposed resolution:


      1. Remove 17.11.5 [support.initlist.range]:

template<class E> constexpr const E* begin(initializer_list<E> il) noexcept;
-1- Returns: il.begin().
template<class E> constexpr const E* end(initializer_list<E> il) noexcept;
-2- Returns: il.end()

      2. Modify 17.11.2 [initializer.list.syn] as indicated:

// all freestanding
namespace std {
  template<class E> class initializer_list {
  public:
    using value_type      = E;
    using reference       = const E&;
    using const_reference = const E&;
    using size_type       = size_t;

    using iterator        = const E*;
    using const_iterator  = const E*;

    constexpr initializer_list() noexcept;

    constexpr size_t size() const noexcept;     // number of elements
    constexpr const E* begin() const noexcept;  // first element
    constexpr const E* end() const noexcept;    // one past the last element
  };

  // [support.initlist.range], initializer list range access
  template<class E> constexpr const E* begin(initializer_list<E> il) noexcept;
  template<class E> constexpr const E* end(initializer_list<E> il) noexcept;
}

I tried submitting it as a standard library issue, but the LWG chair said it needed a proposal. Can someone help?

--
Std-Proposals mailing list
Std-Proposals_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2022-08-30 14:07:58