There is a implementation quite similar to std::initializer_list
used in an environment where standart C++ library is not available:
template<typename T>
class initializer_list {
public:
using value_type = T;
using reference = const T &;
using const_reference = const T &;
using size_type = size_t;
using iterator = const T *;
using const_iterator = const T *;
private:
iterator m_array;
size_type m_length;
constexpr initializer_list( const_iterator array, size_type length ) noexcept : m_array( array ), m_length( length ) {}
public:
constexpr initializer_list( void ) noexcept : m_array( nullptr ), m_length( 0 ) {}
/* Number of elements */
constexpr size_type size( void ) const noexcept {
return m_length;
}
/* First element */
constexpr const_iterator begin( void ) const noexcept {
return m_array;
}
/* One past the last element */
constexpr const_iterator end( void ) const noexcept {
return begin() + size();
}
};
template<typename T>
constexpr const T * begin( initializer_list<T> list ) noexcept {
return list.begin();
}
template<typename T>
constexpr const T * end( initializer_list<T> list ) noexcept {
return list.end();
}
Then such initializer_list<T>
is about to be used in another class constructor:
template<typename T>
struct user {
user( initializer_list<T> init_values ) { ... }
};
and the intension to use both things together:
user<int> sample { 1, 2, 3, 4, 5 };
Obviously, the compiler does not know how to deduce the type of the brace initializer list so that it uses initializer_list as implemented above. I suppose some kind of deduction guide shall be implemented to connect my implementation of initializer_list and the brace initializer list. But I have no clue how to implement such.
Can someone advise me how to implement the described deduction guide?