#include
#include
class MyClass
{
private:
int x;
double d;
public:
MyClass(int xx, double dd)
: x{ xx }, d{ dd } {}
void printdata() { std::cout << "x: " << x << ", d: " << d; }
};
int main()
{
auto p = std::make_unique(123, 456.789);
p->printdata();
}
3 C++17
The C++17 standard introduces new language and library features and changes some of the language rules.
17.1 Nested Namespaces
Remember how we said we could have nested namespaces ? We can put a namespace into another namespace. We used the following the nest namespaces:
namespace MyNameSpace1
{
namespace MyNameSpace2
{
namespace MyNameSpace3
{
// some code
}
}
}
The C++17 standard allows us to nest namespaces using the namespace resolution operator. The above example can now be rewritten as:
namespace MyNameSpace1::MyNameSpace2::MyNameSpace3
{
// some code
}
17.2 Constexpr Lambdas
Lambdas can now be a constant expression, meaning they can be evaluated during compile-time:
int main()
{
constexpr auto mylambda = [](int x, int y) { return x + y; };
static_assert(mylambda(10, 20) == 30, "The lambda condition is not true.");
}
An equivalent example where we put the constexpr specifier in the lambda itself, would be:
int main()
{
auto mylambda = [](int x, int y) constexpr { return x + y; };
static_assert(mylambda(10, 20) == 30, "The lambda condition is not true.");
}
This was not the case in earlier C++ standards.
17.3 Structured Bindings
Structured binding binds the variable names to elements of compile-time known expressions, such as arrays or maps. If we want to have multiple variables taking values of expression elements, we use the structured bindings. The syntax is:
auto [myvar1, myvar2, myvar3] = some_expression;
A simple example where we bound three variables to be aliases for three array elements would be:
int main()
{
int arr[] = { 1, 2, 3 };
auto [myvar1, myvar2, myvar3] = arr;
}
Now we have defined three integer variables. These variables have array elements values of 1, 2, 3, respectively. These variables are copies of array elements. Making changes to variables does not affect the array elements themselves:
#include
int main()
{
int arr[] = { 1, 2, 3 };
auto [myvar1, myvar2, myvar3] = arr;
myvar1 = 10;
myvar2 = 20;
myvar3 = 30;
for (auto el : arr)
{
std::cout << el << ' ';
}
}
We can make structured bindings of reference type by using the auto& syntax. This means the variables are now references to array elements and making changes to variables also changes the array elements:
#include
int main()
{
int arr[] = { 1, 2, 3 };
auto& [myvar1, myvar2, myvar3] = arr;
myvar1 = 10;
myvar2 = 20;
myvar3 = 30;
for (auto el : arr)
{
std::cout << el << ' ';
}
}
It is an excellent way of introducing and binding multiple variables to some container-like expression elements.
17.4 std::filesystem
The std::filesystem library allows us to work with files, paths, and folders on our system. The library is declared through a header. Paths can represent paths to files and paths to folders. To check if a given folder exists, we use: