Uniform Initialization in C++

Uniform initialization is a feature in C++ 11 that allows the usage of a consistent syntax to initialize variables and objects ranging from primitive type to aggregates. In other words, it introduces brace-initialization that uses braces ({}) to enclose initializer values. The syntax is as follows:

type var_name{arg1, arg2, ....arg n}

Following are some of the examples of the different ways of initializing different types:

// uninitialized built-in type
int i;    

// initialized built-in type
int j=10; 

// initialized built-in type
int k(10);

// Aggregate initialization
int a[]={1, 2, 3, 4} 

// default constructor
X x1; 

// Parametrized constructor
X x2(1); 

// Parametrized constructor with single argument
X x3=3; 

// copy-constructor
X x4=x3; 

If initialized using brace initialization, the above code can be re-written as:



int i{};     // uninitialized built-in type

int j{10}; // initialized built-in type

int a[]{1, 2, 3, 4} // Aggregate initialization

X x1{}; // default constructor

X x2{}; // Parametrized constructor;

X x4{x3}; // copy-constructor

Applications of Uniform Initialization

  1. Initialization of dynamically allocated arrays:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to demonstrate initialization
    // of dynamic array in C++ using uniform initialization
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        // declaring a dynamic array
        // and initializing using braces
        int* pi = new int[5]{ 1, 2, 3, 4, 5 };
      
        // printing the contents of the array 
        for (int i = 0; i < 5; i++)
            cout << *(pi + i); 
    }

    chevron_right

    
    

    Output:

    12345
    
  2. Initialization of an array data member of a class:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to initialize
    //  an array data member of a class
    // with uniform initialization
    #include <iostream>
    using namespace std;
      
    class A {
        int arr[3];
      
        public:
            // initializing array using uniform initialization
            A(int x, int y, int z)
                : arr{ x, y, z } {};
         
        void show()
        {
            // printing the contents of the array
            for (int i = 0; i < 3; i++)
                cout << *(arr + i);
        }
    };
      
    int main()
    {
        // New object created and the numbers
        // to initialize the array with, are passed
        // into it as arguments
        A a(1, 2, 3);
        a.show();
        return 0;
    }

    chevron_right

    
    

    Output:

    123
    
  3. Implicitly initialize objects to return:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to implicitly 
    // initialize an object to return
    #include <iostream>
    using namespace std;
      
    // declaring a class 'A'
    class
    {
        // a and b are data members
        int a;
        int b;
      
        // constructor
    public:
        A(int x, int y)
            : a(x), b(y)
        {
        }
        void show()
        {
            cout << a << " " << b;
        }
    };
      
    A f(int a, int b)
    {
        // The compiler automatically
        // deduces that the constructor
        // of the class A needs to be called
        // and the function parameters of f are
        // needed to be passed here
        return { a, b };
    }
    int main()
    {
        A x = f(1, 2);
        x.show();
        return 0;
    }

    chevron_right

    
    

    Output:

    1 2
    
  4. Implicitly initialize function parameter
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to demonstrate how to 
    // initialize a function parameter 
    // using Uniform Initialization
      
    #include <iostream>
    using namespace std;
      
    // declaring a class 'A'
    class A {
      
        // a and b are data members
        int a;
        int b;
      
    public:
        A(int x, int y)
            : a(x), b(y)
        {
        }
        void show()
        {
            cout << a << " " << b;
        }
    };
      
    void f(A x)
    {
        x.show();
    }
      
    int main()
    {
      
        // calling function and initializing it's argument
        // using brace initialization
        f({ 1, 2 });
        return 0;
    }

    chevron_right

    
    

    Output:

    1 2
    


My Personal Notes arrow_drop_up

Intern Technical Content Writing at GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.