std::initializer_list
: Brace Initialization, Implementor’s View¶
std::initializer_list
¶
Used to implement brace initialization of custom datatypes, like
std::vector
std::vector<int> ints{1, 2, 3}; std::vector<std::pair<int, std::string>> pairs{ {1, "one"}, {2, "two"} };
When the compiler sees elements in braces, it creates a
std::initializer_list<T>
of those elements (deducing whatever types are necessary)Very lightweight ⟶ no copy
Shallow wrapper around compiler-specific internals
Direct Usage¶
Works like any container datatype …
#include <initializer_list>
std::initializer_list<point> points { {1, 2}, {3, 4} };
for (auto p: points)
do_something_with(p);
Custom Class That Supports Brace Initialization¶
struct point
{
int x, y;
};
class PointCloud
{
public:
PointCloud(std::initializer_list<point> points) : _points(points) {}
private:
std::vector<point> _points;
};
PointCloud cloud{ {1, 2}, {2, 3} };