0

Tangentially-related, follow-on beginner question to a question that I had originally posted here.

In short: how (without using any of the standard library's containers) can I access the pointers of type T that point to the first and last element of a list-initialization? In case my explanation is not understandable:

int x = {1, 2, 3, 4, 5}

If I wanted to create a custom class template<typename T> class array<T> {}; that I can list-initialize, it's my understanding that I could leverage std::initializer_list to be able to do this.

My question is: how would I be able to replicate that behavior? It looks like it leverages std::begin() to do this, but (unless I'm mistaken) that still begs the question: how can I write code that says "return a pointer to the first element of a list to be allocated on the stack" and, at the same time, "return a pointer to the first-past-the-last element of a list to be allocated on the stack"?

I realize that there's a significant "reinventing-the-wheel" component to my question (i.e. why shouldn't I go ahead and use std::initializer_list), but I just want to make sure I understand what the standard library is doing.

Community
  • 1
  • 1
crcvd
  • 1,465
  • 1
  • 12
  • 23
  • I dont understand why do you want to re-invent the wheel? – Samer Oct 16 '14 at 21:49
  • I don't understand what behavior you want to replicate. If you want to write your own `initializer_list`, that's not possible. – Anton Savin Oct 16 '14 at 21:56
  • That was exactly what I was wondering, @AntonSavin: thank you. I'd be happy to accept your answer should you choose to post it below. – crcvd Oct 16 '14 at 21:59

1 Answers1

3

If you want to write your own initializer_list, that's not possible. By this I mean that you can't achieve the behavior where a line like this

array<int> a = {1, 2, 3, 4};

would call a contructor like array(my_custom_initializer_list).

This is because std::initializer_list is a special class which is explicitly mentioned in the C++ standard as the argument type for list-initialization.

The relevant quotes are spread all over the text, but the essential parts are:

8.5.4/2 List-initialization:

A constructor is an initializer-list constructor if its first parameter is of type std::initializer_list<E> or reference to possibly cv-qualified std::initializer_list<E> for some type E, and either there are no other parameters or else all other parameters have default arguments

13.3.1.7 Initialization by list-initialization:

When objects of non-aggregate class type T are list-initialized (8.5.4), overload resolution selects the constructor in two phases:
— Initially, the candidate functions are the initializer-list constructors (8.5.4) of the class T and the argument list consists of the initializer list as a single argument.
— If no viable initializer-list constructor is found, overload resolution is performed again, where the candidate functions are all the constructors of the class T and the argument list consists of the elements of the initializer list.

Community
  • 1
  • 1
Anton Savin
  • 40,838
  • 8
  • 54
  • 90