Uniform Initialization In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Uniform Initialization In C++

Uniform Initialization In C++

BLUF: Mastering Uniform Initialization In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Uniform Initialization In C++

C++ is renowned for its efficiency. Learn how Uniform Initialization In C++ enables low-level control and high-performance computing in the tutorial below.

Uniform Initialization:

The process of initializing and assigning values to objects has seen significant progress and enhancements within the realm of C++ programming. The introduction of uniform initialization in C++11 marked a crucial advancement towards a standardized and intuitive syntax for initialization. This feature aimed to simplify the initialization of objects across various data types and containers. By providing a clear and uniform method, uniform initialization brings benefits in terms of compatibility, adaptability, and code readability.

In C++11, uniform initialization is a functionality enabling the initialization of variables and objects, ranging from basic types to aggregates, through a standardized syntax. Put differently, it introduces brace initialization, where initializer values are enclosed within braces ({}).

Comprehending Uniform Initialization:

Before the release of C++11, alternative methods existed for initializing objects, such as deploying curly braces, brackets, and the traditional assignment operator.

For example:

Example

int number = 10; // Traditional initialization
std::string name("Alice"); // Initialization using parentheses
int numbers[] = {1, 2, 3, 4, 5}; // Initialization of an array using curly braces
  • When working with sophisticated data structures or initializing objects of user-defined types, the disparities in initialization syntax cause confusion and inconsistencies.
  • Braces {} are used on all data types and containers in Uniform Initialization, which was created to standardise these initialization techniques.

The format of uniform initialization employs curly braces {} to uniformly initialize objects, regardless of their data type.

This is an overview of its syntax:

  1. Defining basic data types:
Example

int num{10}; // Initialization of an integer using braces
double pi{3.14}; // Initialization of a double using braces
  1. Initialization of arrays:
Example

int numbers[]{1, 2, 3, 4, 5}; // Initialization of an array using braces
  1. Creating instances of custom-defined data structures (classes and structs):
Example

struct Point {
    int x;
    int y;
};
Point p{5, 10}; // Initialization of a user-defined type using braces
  1. Setting up containers (such as std::vector, std::array, std::map, etc.):
Example

std::vector<int> vec{1, 2, 3, 4, 5}; // Initialization of a vector using braces
std::array<int, 3> arr{{1, 2, 3}}; // Initialization of an array using braces
std::map<std::string, int> ages{{"Alice", 30}, {"Bob", 25}}; // Initialization of a map using braces

Program:

Let's consider a code example to demonstrate the application of uniform initialization in C++:

Example

#include <iostream>
#include <vector>
struct Point {
    int x;
    int y;
};
int main() {
    // Uniform Initialization of fundamental types
    int number{10};
    double pi{3.14};
    std::cout << "Initialized number: " << number << std::endl;
    std::cout << "Initialized pi: " << pi << std::endl;
    // Uniform Initialization of arrays
    int numbers[]{1, 2, 3, 4, 5};
    std::cout << "\nInitialized array: ";
    for (int num: numbers) {
        std::cout << num << " ";
    }
    std::cout << std::endl;
    // Uniform Initialization of user-defined type (Point)
    Point p{5, 10};
    std::cout << "\nInitialized Point - x: " << p.x << ", y: " << p.y << std::endl;
    // Uniform Initialization of a vector
    std::vector<int> vec{1, 2, 3, 4, 5};
    std::cout << "\nInitialized vector: ";
    for (int val: vec) {
        std::cout << val << " ";
    }
    std::cout << std::endl;
    return 0;
}

Output:

Example 2: Implicitly Initialize Function Parameter

Example

// 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(); }
// Driver Code
int main()
{
	// calling function and initializing it's argument
	// using brace initialization
	f({ 1, 2 });
	return 0;
}

Output:

Example 3: Implicitly initialize objects to return

Example

// C++ program to implicitly
// initialize an object to return
#include <iostream>
using namespace std;
// declaring a class 'A'
class A {
	// 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 };
}
// Driver Code
int main()
{
	A x = f(1, 2);
	x.show();
	return 0;
}

Output:

Benefits of Standard Initialization:-

There are several benefits of standard initialization. Some main benefits of the uniform initialization are as follows:

  • Uniformity: Uniform Initialization makes code more consistent and legible across various kinds and containers by bringing uniformity to the object initialization syntax.
  • Prevents unintentional narrowing conversions: It assists in preventing the loss of values that arise from converting one type to another.
  • Initialization of aggregates: By permitting the use of braces {}, it makes initializing aggregate types (arrays, structs, etc.) simpler.
  • Support for initializer lists: It makes it possible to utilise initializer lists, which offer a clear and simple method for initializing user-defined types and containers.
  • Uniform Initialization makes code more consistent and legible across various kinds and containers by bringing uniformity to the object initialization syntax.
  • It assists in preventing the loss of values that arise from converting one type to another.
  • By permitting the use of braces {}, it makes initializing aggregate types (arrays, structs, etc.) simpler.
  • It makes it possible to utilise initializer lists, which offer a clear and simple method for initializing user-defined types and containers.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience