Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #88008

Re: initializer_list constructor implementation

From Bo Persson <bo@bo-persson.se>
Newsgroups comp.lang.c++
Subject Re: initializer_list constructor implementation
Date 2022-12-18 12:33 +0100
Message-ID <k08c43Fh735U1@mid.individual.net> (permalink)
References <5318cc4c-fd12-434d-aa3e-6e4f361241e2n@googlegroups.com>

Show all headers | View raw


On 2022-12-18 at 11:13, Pawel Por wrote:
> Hello,
> 
> I'd like to understand the implementation of std::initializer_list constructor. I've copied the most recent std::lib implementation from git and building it with gcc 9.4.0 -std=c++2a. I get the following error:
> 
> error: no matching function for call to ‘custom_initializer<int>::custom_initializer(<brace-enclosed initializer list>)’
> 
> I don't understand what is the basic mechanism to pass the brace-enclosed list to the constructor.
> Please give me some hints.
> Thank you in advance
> 
> template<class _E>
> class custom_initializer
> {
> public:
>    typedef _E    value_type;
>    typedef const _E&   reference;
>    typedef const _E&   const_reference;
>    typedef size_t    size_type;
>    typedef const _E*   iterator;
>    typedef const _E*   const_iterator;
> 
> private:
>    iterator      _M_array;
>    size_type     _M_len;
> 
>    // The compiler can call a private constructor.
>    constexpr custom_initializer(const_iterator __a, size_type __l)
>    : _M_array(__a), _M_len(__l) { }
> 
> public:
>    constexpr custom_initializer() noexcept
>    : _M_array(0), _M_len(0) { }
> 
>    // Number of elements.
>    constexpr size_type
>    size() const noexcept { return _M_len; }
> 
>    // First element.
>    constexpr const_iterator
>    begin() const noexcept { return _M_array; }
> 
>    // One past the last element.
>    constexpr const_iterator
>    end() const noexcept { return begin() + size(); }
> };
> 
> int main(int argc, char **argv)
> {
>    custom_initializer<int> ppp{1, 2, 3};
>    return 0;
> }

std::initializer_list is "magic" in that the compiler knows about it and 
its connection to brace initializers.

Your class lacks this compiler magic, so works differently (=not supported).

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

initializer_list constructor implementation Pawel Por <porparek@gmail.com> - 2022-12-18 02:13 -0800
  Re: initializer_list constructor implementation Bo Persson <bo@bo-persson.se> - 2022-12-18 12:33 +0100
    Re: initializer_list constructor implementation Pawel Por <porparek@gmail.com> - 2022-12-18 12:40 -0800
      Re: initializer_list constructor implementation Bo Persson <bo@bo-persson.se> - 2022-12-19 10:49 +0100
        Re: initializer_list constructor implementation Muttley@dastardlyhq.com - 2022-12-19 09:59 +0000

csiph-web