Using Initializer Lists in Custom Containers

How can we use std::initializer_list to initialize custom container types?

std::initializer_list is a powerful tool for initializing custom container types, providing a flexible and intuitive way to pass a list of values.

To use std::initializer_list in your custom containers, you need to define a constructor that accepts an initializer_list and use it to initialize your container's internal storage.

Here's an example of a simple custom container that uses std::initializer_list:

#include <iostream>
#include <vector>

template <typename T>
class CustomContainer {
public:
  CustomContainer(std::initializer_list<T> initList)
    : data{initList} {}

  void Log() const {
    for (const auto& elem : data) {
      std::cout << elem << ' ';
    }
  }

private:
  std::vector<T> data;
};

int main() {
  CustomContainer<int> myContainer{
    1, 2, 3, 4, 5
  }; 
  myContainer.Log();
}
1 2 3 4 5

In this example, CustomContainer is a template class that accepts a type parameter T. The constructor takes an std::initializer_list<T> and uses it to initialize a std::vector<T> which serves as the internal storage.

When you create an instance of CustomContainer, you can use brace-enclosed lists to initialize it, just like with standard containers. This makes your custom container more intuitive to use.

You can also use std::initializer_list to forward values to another container or process them as needed. Here's another example where we initialize a container with a sum of all elements:

#include <iostream>
#include <numeric>
#include <vector>

class SumContainer {
public:
  SumContainer(std::initializer_list<int> initList)
    : sum{std::accumulate(
      initList.begin(), initList.end(), 0
    )} {}

  void Log() const {
    std::cout << "Sum: " << sum;
  }

private:
  int sum;
};

int main() {
  SumContainer myContainer{1, 2, 3, 4, 5}; 
  myContainer.Log();
}
Sum: 15

In this case, SumContainer takes an std::initializer_list<int> and calculates the sum of its elements. This value is stored in a private member and logged to the console.

Using std::initializer_list in custom containers enhances flexibility and makes initialization straightforward, improving the overall usability of your container types.

List, Aggregate, and Designated Initialization

A quick guide to creating objects using lists, including std::initializer_list, aggregate and designated initialization

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

List Initialization with Private Members
Can we use list initialization for classes with private or protected members?
Aggregate Initialization for Nested Structures
How does aggregate initialization work for complex types with nested structures?
Designated Initializers with Private Members
Can designated initializers be used with types that have private or protected members?
Aggregate Initialization with Inheritance
How does aggregate initialization interact with inheritance and derived classes?
Heterogeneous Initializer Lists
Can std::initializer_list be used with heterogeneous data types in any way?
List Initialization with constexpr and constinit
How does list initialization interact with constexpr and constinit specifiers in C++20?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant