๐๐ง๐๐๐ซ ๐ญ๐ก๐ ๐๐จ๐จ๐: ๐๐ฑ๐ฉ๐ฅ๐จ๐ซ๐ข๐ง๐ ๐๐ง๐ข๐ญ๐ข๐๐ฅ๐ข๐ณ๐๐ซ-๐๐ข๐ฌ๐ญ ๐๐จ๐ง๐ฌ๐ญ๐ซ๐ฎ๐๐ญ๐จ๐ซ๐ฌ ๐ข๐ง ๐++
โAvoid initializing containers with initializer_list if you canโ
๐ฏEver wondered what really happens under the hood when a C++ container uses an initializer-list constructor that accepts an std::initializer_list type?
๐ด๐๐ก๐๐ญ ๐ข๐ฌ ๐ฌ๐ญ๐::๐ข๐ง๐ข๐ญ๐ข๐๐ฅ๐ข๐ณ๐๐ซ_๐ฅ๐ข๐ฌ๐ญ<T>?
The standard-library type std::initializer_list<T> is used to handle variable-length {}-lists. Its ๐ฆ๐จ๐ฌ๐ญ ๐จ๐๐ฏ๐ข๐จ๐ฎ๐ฌ ๐ฎ๐ฌ๐ is to to enable initializer lists for user-defined containers, though it can also be used directly. When we use a {}-list, such as {1, 2, 3}, the compiler creates an object of type std::initializer_list<T> to pass to the constructor.
๐Links to a simplified container implementation demonstrating ๐จ๐ง๐ฅ๐ฒ an initializer-list constructor (skipping edge cases and exception handling is intentional to make the example concise).
โ
godbolt.org/z/MW4c7ThMh
โ
godbolt.org/z/h1j1KbGf9๐Iโd like to highlight some key concepts based on the follwoing code:
#include <memory>
template <typename T>
class Container
{
public:
Container(std::initializer_list<T> lst):
m_size{ lst.size() },
m_data{ static_cast<T*>(::operator new(m_size * sizeof(T))) }
{
for (size_t i = 0; auto & item: lst)
{
// ๐ฉ๐ฅ๐๐๐๐ฆ๐๐ง๐ญ ๐ง๐๐ฐ
new (std::addressof(m_data[i++])) T{ item };
}
}
~Container()
{
if (m_data)
{
for (size_t i = m_size; i-- > 0; )
{
m_data[i].~T();
}
::operator delete(m_data);
m_data = nullptr;
}
}
private:
size_t m_size;
T* m_data;
};1๏ธโฃ๐จ๐ฉ๐๐ซ๐๐ญ๐จ๐ซ ๐ง๐๐ฐ is used โ not to be confused with the ๐ง๐๐ฐ ๐จ๐ฉ๐๐ซ๐๐ญ๐จ๐ซ โ to allocate raw memory for type ๐ without invoking its constructor. This is especially useful when ๐ lacks a default constructor or has multiple overloaded constructors, because it allows us to selectively initialize objects via placement new based on the provided initializer list.
2๏ธโฃinitializer_list doesnโt support subscripting but offers begin(), end(), size() members. Inside the for-each loop, placement new ๐๐จ๐ฉ๐ฒ ๐๐จ๐ง๐ฌ๐ญ๐ซ๐ฎ๐๐ญ๐ฌ an object of type ๐ with the value ๐๐๐๐ at the specified address in ๐_๐ ๐๐๐. This is where the actual copying of elements from the initializer_list into the containerโs internal memory occurs. This use of a constructor is known as ๐ฉ๐ฅ๐๐๐๐ฆ๐๐ง๐ญ ๐ง๐๐ฐ.
3๏ธโฃTypically, it is unnecessary and potentially error-prone to explicitly call a destructor. But, there are rare but important cases where a destructor must be called explicitly, such as in the destructor of the ๐๐จ๐ง๐ญ๐๐ข๐ง๐๐ซ class. Here, the notation ๐ฆ_๐๐๐ญ๐[๐ข].~๐() is used to explicitly invoke Tโs destructor for each element in the container. Eventually we use ๐จ๐ฉ๐๐ซ๐๐ญ๐จ๐ซ ๐๐๐ฅ๐๐ญ๐ โ not to be confused with ๐ญ๐ก๐ ๐๐๐ฅ๐๐ญ๐ ๐จ๐ฉ๐๐ซ๐๐ญ๐จ๐ซ. Operator delete does not implicitly call destructors; it only deallocates memory.
Get Samiraโs stories inย yourย inbox
Join Medium for free to get updates fromย thisย writer.
๐ดDestruction Process in the custom Container class:
โ Explicit Destructor Calls for Each Element
The loop iterates over the elements in reverse order (which can be important if the destruction order matters) and explicitly calls each elementโs destructor using m_data[i].~T();. This ensures that if T manages any resources (like dynamically allocated memory, file handles, etc.), its destructor will properly release those resources.
โ Deallocation of the Raw Memory
After all the elements have been properly destroyed, ::operator delete(m_data); is called. This frees the memory that was previously allocated with ::operator new(m_size * sizeof(T)) in the constructor. So, the destructor first makes sure that each T instance cleans up its internal resources, and then it releases the raw memory used to store these instances.
๐You might be wondering ๐ฐ๐ก๐ฒ ๐ฐ๐ ๐๐จ๐งโ๐ญ ๐ฎ๐ฌ๐ ๐ฌ๐ญ๐::๐ฆ๐จ๐ฏe ๐ญ๐จ ๐๐ฏ๐จ๐ข๐ ๐๐จ๐ฉ๐ฒ๐ข๐ง๐ ๐ข๐ง ๐ญ๐ก๐ ๐๐ข๐ซ๐ฌ๐ญ ๐ฉ๐ฅ๐๐๐:
new (std::addressof(m_data[i++])) T{๐ฌ๐ญ๐::๐ฆ๐จ๐ฏe(item)};
Because elements in an initializer_list are ๐ข๐ฆ๐ฆ๐ฎ๐ญ๐๐๐ฅ๐, we cannot apply a move constructor to them. Using std::move would simply fall back to copying the elements!
๐Now, if you click on the link and run the code, youโll notice the differences between using initializer_list, push_back, and emplace_back in a container like std::vector. Particularly, initializer_list incurs ๐๐ฑ๐ญ๐ซ๐ ๐๐จ๐ฉ๐ข๐๐ฌ!
#cpp #iLoveCpp #cplusplus
โก๏ธ Resources :
The C++ Programming Language by Bjarne Stroustrup 4th edition.






