Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it better to use std::make_* instead of the constructor?

Tags:

People also ask

Why should I use Make_unique?

It is recommended to use the 'make_unique/make_shared' function to create smart pointers. The analyzer recommends that you create a smart pointer by calling the 'make_unique' / 'make_shared' function rather than by calling a constructor accepting a raw pointer to the resource as a parameter.

Why do STDS make a pair?

The difference is that with std::pair you need to specify the types of both elements, whereas std::make_pair will create a pair with the type of the elements that are passed to it, without you needing to tell it.

Why do we use Make_pair in C++?

The make_pair() function, which comes under the Standard Template Library of C++, is mainly used to construct a pair object with two elements. In other words, it is a function that creates a value pair without writing the types explicitly.

Does Make_unique use new?

The addition of make_unique finally means we can tell people to 'never' use new rather than the previous rule to “'never' use new except when you make a unique_ptr”.


There are some functions in the STL which start with the make_ prefix like std::make_pair, std::make_shared, std::make_unique etc. Why is it a better practice to use them instead of simply using the constructor ?

auto pair2 = std::pair< int, double >( 1, 2.0 );
auto pair3 = std::make_pair( 1, 2.0 );

std::shared_ptr< int > pointer1 = std::shared_ptr< int >( new int( 10 ) );
std::shared_ptr< int > pointer2 = std::make_shared< int >( 10 );
  • I just see that these functions make the code a little shorter, but is that all ?
  • Are there any other advantages ?
  • Are these functions safer to use ?