Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
C++Code

Learn How To Employ C++ Initializer Lists For Reliable Windows Development With C++ Builder

Initializer lists is a feature that has been extended in C++11 standard. An initializer list represents a list of ordered arguments, in curly brackets, that is used to initialize structures, classes and arrays. Until C++ 11, only classes that conformed to the Plain Old Data (POD) definition could be initialized using initializer lists. Now, classes like std::vector can also be initialized using initializer lists. This is done by using a template named std::initializer_list<> that can be passed as an argument to constructors or other functions.

There are several cases where the use of an initializer list is absolutely necessary, these include:

-Initializing a reference type data member.
-Initializing const data member.
-Initializing member objects which do not have a default constructor.

Introducing C++11 Brace-Initialization

C++11 attempts to overcome the problems of C++03 initialization by introducing a universal initialization notation that applies to every type—whether a POD variable, a class object with a user-defined constructor, a POD array, a dynamically allocated array, or even a Standard Library container. The universal initializer is called a brace-init. It looks like this:

An empty pair of braces indicates default initialization. Default initialization of POD types usually means initialization to binary zeros, whereas for non-POD types default initialization means default construction:

C++11 class member initializers are mostly a matter of convenience. They provide an apparent and simplified form of initializing data members. A class member initializer can consist of any valid initialization expression, whether that’s the traditional equal sign, a pair of parentheses, or the new brace-init:

Initializer Lists and Sequence Constructors

An initializer list lets you use a sequence of values wherever an initializer can appear. For example, you can initialize a vector in C++11 like this:

C++11 furnishes every STL container with a new constructor type called a sequence constructor. A sequence constructor intercepts initializers in the form of {x,y…}. To make this machinery work, C++11 introduced another secret ingredient: an auxiliary class template called std::initializer_list<T>. When the compiler sees an initializer list, say {0.5, 1.33, 2.66}, it transforms the values in that list into an array of T with n elements (n is the number of values enclosed in braces) and uses that array to populate an implicitly generated initializer_list<T> object. The class template initializer_list has three member functions that access the array:

Head over and check out more about initializer lists and more of the modern C++ features available in C++Builder.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Related posts
C++Delphi

Measure Heart Rate Using Delphi And C++ Builder With The Power Of ThingConnect IoT Device Component Pack

C++Delphi

Create Enterprise-Grade WebSockets Based Apps With Powerful Components For Delphi

DelphiShowcase

Incredibly Powerful Enhanced Terminal And Network Toolkit For Windows Is Built In Delphi

C++CodeRAD Studio

Learn How To Use C++ Explicit Virtual Overrides In Windows Development

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES