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


Groups > comp.lang.c++ > #88007 > unrolled thread

initializer_list constructor implementation

Started byPawel Por <porparek@gmail.com>
First post2022-12-18 02:13 -0800
Last post2022-12-19 09:59 +0000
Articles 5 — 3 participants

Back to article view | Back to comp.lang.c++


Contents

  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

#88007 — initializer_list constructor implementation

FromPawel Por <porparek@gmail.com>
Date2022-12-18 02:13 -0800
Subjectinitializer_list constructor implementation
Message-ID<5318cc4c-fd12-434d-aa3e-6e4f361241e2n@googlegroups.com>
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;
}

[toc] | [next] | [standalone]


#88008

FromBo Persson <bo@bo-persson.se>
Date2022-12-18 12:33 +0100
Message-ID<k08c43Fh735U1@mid.individual.net>
In reply to#88007
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).

[toc] | [prev] | [next] | [standalone]


#88014

FromPawel Por <porparek@gmail.com>
Date2022-12-18 12:40 -0800
Message-ID<0aeb40fa-6829-4123-94bc-ed718dee6814n@googlegroups.com>
In reply to#88008
> 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).

Thank you for response. Is this "magic" somehow available for the users or totally hidden. If so, how can I use it ?

[toc] | [prev] | [next] | [standalone]


#88037

FromBo Persson <bo@bo-persson.se>
Date2022-12-19 10:49 +0100
Message-ID<k0aqcqFs99vU1@mid.individual.net>
In reply to#88014
On 2022-12-18 at 21:40, Pawel Por wrote:
>> 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).
> 
> Thank you for response. Is this "magic" somehow available for the users or totally hidden. If so, how can I use it ?

I think it is just triggered by the name std::initializer_list. So not 
really available for user code.

[toc] | [prev] | [next] | [standalone]


#88038

FromMuttley@dastardlyhq.com
Date2022-12-19 09:59 +0000
Message-ID<tnpcl6$580$1@gioia.aioe.org>
In reply to#88037
On Mon, 19 Dec 2022 10:49:14 +0100
Bo Persson <bo@bo-persson.se> wrote:
>On 2022-12-18 at 21:40, Pawel Por wrote:
>>> 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).
>
>> 
>> Thank you for response. Is this "magic" somehow available for the users or
>totally hidden. If so, how can I use it ?
>
>I think it is just triggered by the name std::initializer_list. So not 
>really available for user code.

A bit like printf() is usually a compiler built-in which means it can give
warnings about mismatched format specifiers and parameter types, which it
won't do with user written vararg functions.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web