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

06 Mar, 2019
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:
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.#include <iostream>using namespace std;  // Driver Codeint 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;}Output:a = 5
b = 10
ExplanationIn 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.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.#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;}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};
Outputprog.cpp: In function 'int main()':
prog.cpp:9:12: error: scalar object 'b' requires one element in initializer
 int b={7, 12};
            ^

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.#include <iostream>using namespace std;  // Driver Codeint 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;}Output:a = 5
b = 10
ExplanationIn 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.
#include <iostream>using namespace std;  // Driver Codeint 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;}
Output:
a = 5
b = 10

ExplanationIn 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.
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.#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;}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};
Outputprog.cpp: In function 'int main()':
prog.cpp:9:12: error: scalar object 'b' requires one element in initializer
 int b={7, 12};
            ^

#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;}
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};
            ^

cpp-advanced
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Iterators in C++ STL
Operator Overloading in C++
Friend class and function in C++
Polymorphism in C++
Sorting a vector in C++
Convert string to char array in C++
Inline Functions in C++
List in C++ Standard Template Library (STL)
std::string class in C++
new and delete operators in C++ for dynamic memory