Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

initializer_list constructor implementation

24 views
Skip to first unread message

Pawel Por

unread,
Dec 18, 2022, 5:13:22 AM12/18/22
to
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;
}

Bo Persson

unread,
Dec 18, 2022, 6:33:43 AM12/18/22
to
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).


Pawel Por

unread,
Dec 18, 2022, 3:40:42 PM12/18/22
to
> 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 ?

Bo Persson

unread,
Dec 19, 2022, 4:49:28 AM12/19/22
to
I think it is just triggered by the name std::initializer_list. So not
really available for user code.

Mut...@dastardlyhq.com

unread,
Dec 19, 2022, 4:59:16 AM12/19/22
to
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.

0 new messages