How to make brace enclosed initializer list constructor

Hello, I'm trying to make a class which stores an int array, and I want to input it to the constructor like this:

 Example test = {3,4,5,6,7};

But I dont know how to make a constructor which can have this input. Any help?

With this syntax:

Example test = {3,4,5,6,7};

3 implementations are accepted.

  1. Your class must have at least 5 members. Each member will be initialize in order from top to bottom (provided that there’s no user defined constructor).
  2. A standard constructor that takes 5 arguments.
  3. A constructor that takes “std::initializer_list<>” as an argument.

If you implement both 2 and 3, 2 will have priority.

A fourth option: the class has a single public member of type int [5] and no user-provided constructor.
This is how std::array is implemented.

std::vector uses std::initializer_list. The problem is that there's no way to check the number of elements at compile-time.

Pieter

arduino_new:
3) A constructor that takes “std::initializer_list<>” as an argument.

I tried to do this, as it’s the best solution for my case, but the compiler says that std::initializer_list is not defined, even if I try to pass in a class for the template

xdddman:
I tried to do this, as it’s the best solution for my case, but the compiler says that std::initializer_list is not defined, even if I try to pass in a class for the template

#include <initializer_list>

class Example {
  private:
  public:
    Example(std::initializer_list<int> list) {
    }
};

Example exam = {10,10,10,10,10};
void setup() {
  // put your setup code here, to run once:
  
}

void loop() {
  // put your main code here, to run repeatedly:

}

I tried the code you posted and the compiler says initializer_list does not exist

Did you include the header?

The code I posted compiled for ESP board but not for Uno. It seems like AVR boards do not have stl libraries.

I tried the same code with my ESP and it worked fine, so it is that the AVR boards dont include stl. Do you know of any other way I could implement initializer_list or of making the constructor?

xdddman:
I tried the same code with my ESP and it worked fine, so it is that the AVR boards dont include stl. Do you know of any other way I could implement initializer_list or of making the constructor?

I don't have an idea how it's implemented. But there are several custom stl libraries for avr boards if you want to search and pick one.

Alright! Thanks for the help!

This should work:

[color=#00979c]class[/color] [color=#000000]Example[/color] [color=#000000]{[/color]
  [color=#00979c]public[/color][color=#434f54]:[/color]
    [color=#00979c]int[/color] [color=#00979c]array[/color][color=#000000][[/color][color=#000000]5[/color][color=#000000]][/color][color=#000000];[/color]

    [color=#00979c]void[/color] [color=#d35400]print[/color][color=#000000]([/color][color=#000000])[/color] [color=#00979c]const[/color] [color=#000000]{[/color]
      [color=#5e6d03]for[/color] [color=#000000]([/color][color=#00979c]int[/color] [color=#000000]elem[/color] [color=#434f54]:[/color] [color=#00979c]array[/color][color=#000000])[/color]
        [b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]println[/color][color=#000000]([/color][color=#000000]elem[/color][color=#000000])[/color][color=#000000];[/color]
    [color=#000000]}[/color]
[color=#000000]}[/color][color=#000000];[/color]

[color=#000000]Example[/color] [color=#000000]test[/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#000000]3[/color][color=#434f54],[/color] [color=#000000]4[/color][color=#434f54],[/color] [color=#000000]5[/color][color=#434f54],[/color] [color=#000000]6[/color][color=#434f54],[/color] [color=#000000]7[/color][color=#000000]}[/color][color=#000000];  [/color]

[color=#00979c]void[/color] [color=#5e6d03]setup[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
  [b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]begin[/color][color=#000000]([/color][color=#000000]115200[/color][color=#000000])[/color][color=#000000];[/color]
  [color=#5e6d03]while[/color] [color=#000000]([/color][color=#434f54]![/color][b][color=#d35400]Serial[/color][/b][color=#000000])[/color][color=#000000];[/color]
  [color=#000000]test[/color][color=#434f54].[/color][color=#d35400]print[/color][color=#000000]([/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]}[/color]

[color=#00979c]void[/color] [color=#5e6d03]loop[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color][color=#000000]}[/color]