Uniform Initialization in C++
Last Updated :
13 Feb, 2023
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;
// Parameterized constructor
X x2(1);
// Parameterized 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{}; // initialized built-in type, equals to int i{0};
int j{10}; // initialized built-in type
int a[]{1, 2, 3, 4} // Aggregate initialization
X x1{}; // default constructor
X x2{1}; // Parameterized constructor;
X x4{x3}; // copy-constructor
Applications of Uniform Initialization
Initialization of dynamically allocated arrays:
C++
// 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) << " ";
}
Time Complexity: O(1)
Auxiliary Space: O(1)
Initialization of an array data member of a class:
C++
// 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) <<" ";
}
};
// Driver Code
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;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
Implicitly initialize objects to return:
C++
// 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;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
Implicitly initialize function parameter
C++
// 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;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Aggregate Initialization in C++ 20
C++20 has undergone several upgrades that aim to streamline and enhance the code. Among these improvements lies a remarkable modification that simplifies the initialization process of aggregates, such as arrays, structs, and classes lacking user-declared constructors. In C++20, you can use aggregate
3 min read
Temporary Materialization in C++ 17
C++ is a popular programming language that is widely used in the development of system software, application software, game engines, and more. One of the most significant improvements in C++17 is the introduction of temporary materialization, a feature that helps reduce the complexity of code and im
3 min read
transform() in C++ STL
In C++, transform() is a built-in STL function used to apply the given operation to a range of elements and store the result in another range. Letâs take a look at a simple example that shows the how to use this function:C++#include <bits/stdc++.h> using namespace std; int main() { vector<i
4 min read
std::initializer_list in C++ 11
The std::initializer_list class template was added in C++ 11 and contains many built-in functions to perform various operations with the initializer list. It provides member functions like a size(), begin(), end(), and constructor to construct, iterate, and access elements of the initializer list. T
6 min read
Partial Template Specialization in C++
In C++, template specialization enables us to define specialized versions of templates for some specific argument patterns. It is of two types: Full Template SpecializationPartial Template SpecializationIn this article, we will discuss the partial template specialization in C++ and how it is differe
3 min read
ios manipulators unitbuf() function in C++
The unitbuf() method of stream manipulators in C++ is used to set the unitbuf format flag for the specified str stream. This flag flushes the associated buffer after each operation. Syntax: ios_base& unitbuf (ios_base& str) Parameters: This method accepts str as a parameter which is the stre
2 min read
partition_point in C++
partition_point() Gets the partition point : Returns an iterator to the first element in the partitioned range [first, last) for which pred(predicate) is not true, indicating its partition point. The elements in the range shall already be partitioned, as if partition had been called with the same ar
4 min read
transform_inclusive_scan() function in C++
transform_inclusive_scan() is inbuilt function in C++ and its same as inclusive_scan(), except a unary function which is first applied to each input item.Its functionality is to transform each and every element between first and last with unary_op then computes an inclusive prefix sum operation by t
3 min read
unordered_multimap operator= in C++
The unordered_multimap::operator= is a built-in function in C++ STL which does three types of tasks which are explained below. Syntax (copying elements from different container) : unordered_multimap_name1 operator= (unordered_multimap_name2)Parameters: The function does not accepts any parameter. Th
4 min read
C++ Floating Point Manipulation
Like integers, C++11 introduced some basic inbuilt functions for handling simple mathematical computations of floating-point numbers necessary for day-to-day programming as well as competitive programming. These functions belong to the <cmath> header file. This article discusses some of the fu
6 min read