List, Aggregate, and Designated Initialization

Using Initializer Lists in Custom Containers

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

Abstract art representing computer programming

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.

This Question is from the Lesson:

List, Aggregate, and Designated Initialization

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

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

List, Aggregate, and Designated Initialization

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

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved