Skip to main content

Posts

Showing posts with the label Cpp 17

Template Argument Deduction

  Template Argument Deduction is the ability of templated classes to determine the type of the passed arguments for constructors without explicitly stating the type. Before C++17 , to construct an instance of a templated class we had to explicitly state the types of the argument (or use one of the make_xyz support functions). std::pair p(2, 4.5); Here, p is an instance of the class pair and is initialized with values of 2 and 4.5. Or the other method of achieving this would be: auto p = std::make_pair(2, 4.5); Both methods have their drawbacks. Creating “make functions” like std::make_pair is confusing, artificial and inconsistent with how non-template classes are constructed. std::make_pair, std::make_tuple etc are available in the standard library, but for user-defined types it is worse: you have to write your own make_… functions. In C++17,  This requirement for specifying the types for a templated class constructor has been abolished. This means...

Structured Bindings

Returning multiple Values from function C++ 11 vs C++ 17 Returning compound objects Iterating over a compound collection Direct initialization Returning multiple Values from function C++ 11 vs C++ 17 :  C++ 11 (std::tie): std::tuple mytuple() {     char a = 'a';     int i = 123;     bool b = true;     return std::make_tuple(a, i, b);  // packing variable into tuple } To access return value using C++ 11, we would need something like: char a; int i; bool b; std::tie(a, i, b) = mytuple();  // unpacking tuple into variables Where the variables have to be defined before use and the types known in advance. C++ 17 : auto [a, i, b] = mytuple(); Returning compound objects :  This is the easy way to assign the individual parts of a compound type (such as a struct, pair etc) to different variables all in one go – and have the correct types automatically assigned. So let’s have a look at an ...

C++ 17 Introduction

C++17 has brought a lot of features to the C++ language. Let’s dig into three of them that help make coding easier, more concise, intuitive and correct. We’ll begin with Structured Bindings. These were introduced as a means to allow a single definition to define multiple variables with different types. Structured bindings apply to many situations, and we’ll see several cases where they can make code more concise and simpler. Then we’ll see Template Argument Deduction, which allows us to remove template arguments that we’re used to typing, but that we really shouldn’t need to. And we’ll finish with Selection Initialization, which gives us more control about object scoping and lets us define values where they belong.

std::auto_ptr in c++

Automatic Pointer [deprecated] Note: This class template is deprecated as of C++11 and completely removed in c++ 17. unique_ptr is a new facility with a similar functionality, but with improved security (no fake copy assignments), added features (deleters) and support for arrays. auto_ptr objects have the uniqueness of taking ownership of the pointers allotted to them: An auto_ptr object that has ownership over one element is in charge of destroying the element it points to and to deallocate the memory allocated to it when itself is destroyed. The destructor does this by invoking operator delete automatically. Hence, no two auto_ptr objects should own the same element, since together would try to destruct them at some point. When an assignment operation takes place between two auto_ptr objects, ownership is transferred, which means that the object losing ownership is set to no longer point to the element (it is set to the null pointer). #include void memory_leak(...