C++ 17 | New ways to Assign values to Variables

C++ 17 introduced many new ways to declare a variable. Earlier assignment and declaration was done using “=”

Example:

int a = 5;

But now 2 more ways are introduced in C++17. They are:

  1. Constructor initialization: In this way, the value of the variable is enclosed in parentheses ( () ). In this way, value can be passed in two ways shown below.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
      
    // Driver Code
    int main()
    {
      
        // Declaring the variable in parentheses
        // Method 1
        int a(5);
        cout << "a = " << a;
      
        // Declaring the variable in parentheses
        // Method 2
        int b = (10);
        cout << endl
             << "b = " << b;
      
        return 0;
    }

    chevron_right

    
    

    Output:

    a = 5
    b = 10
    

    Explanation
    In C++ 17, the variable can be declared by providing values through parenthesis too. The difference between constructor initialization and the old normal way of initialization is that it will always return last value in the parenthesis no matter what it’s magnitude or sign is.

    For example,

    a=(2, 21, -50, 1)

    The value stored in a would be 1.

  2. Uniform initialization: In this way, the value of the variable is enclosed in curly braces ( {} ) instead of parentheses. In this way, the value can be passed in two ways shown below.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
      
    int main()
    {
      
        // Declaring the variable in curly braces
        // Method 1
        int a{ 3 };
        cout << "a = " << a;
      
        // Declaring the variable in curly braces
        // Method 2
        int b = { 7 };
        cout << endl
             << "b = " << b;
      
        return 0;
    }

    chevron_right

    
    

    Output:

    a = 3
    b = 7
    

    Unlike Constructor initialization, this assigning method can only take one value in the braces. Providing multiple values would return a compile error.

    For example,

    int b={7, 12};
    

    Output

    prog.cpp: In function 'int main()':
    prog.cpp:9:12: error: scalar object 'b' requires one element in initializer
     int b={7, 12};
                ^
    


My Personal Notes arrow_drop_up

Developer in day, Designer at night GSoC 2019 with Python Software Foundation (EOS Design system)

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 :


Be the First to upvote.


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