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


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

Re: initializer_list constructor implementation

Path csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Bo Persson <bo@bo-persson.se>
Newsgroups comp.lang.c++
Subject Re: initializer_list constructor implementation
Date Sun, 18 Dec 2022 12:33:23 +0100
Lines 59
Message-ID <k08c43Fh735U1@mid.individual.net> (permalink)
References <5318cc4c-fd12-434d-aa3e-6e4f361241e2n@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-Trace individual.net IpeG9nup6vJe9ISg8uzGUw9f8Cidn9YyyszBD6/+2hXEAeidkU
Cancel-Lock sha1:pKjuCE/rXAg4jlXC0IqUNwdcEaw=
User-Agent Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.6.0
Content-Language sv
In-Reply-To <5318cc4c-fd12-434d-aa3e-6e4f361241e2n@googlegroups.com>
Xref csiph.com comp.lang.c++:88008

Show key headers only | 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