Advantages of using std::make_unique over new operator [duplicate]

Advantages make_unique teaches users “never say new/delete and new[]/delete[]” without disclaimers. make_unique shares two advantages with make_shared (excluding the third advantage, increased efficiency). First, unique_ptr<LongTypeName> up(new LongTypeName(args)) must mention LongTypeName twice, while auto up = make_unique<LongTypeName>(args) mentions it once. make_unique prevents the unspecified-evaluation-order leak triggered by expressions like foo(unique_ptr<X>(new X), unique_ptr<Y>(new Y)). (Following the advice … Read more

How does `void_t` work

1. Primary Class Template When you write has_member<A>::value, the compiler looks up the name has_member and finds the primary class template, that is, this declaration: template< class , class = void > struct has_member; (In the OP, that’s written as a definition.) The template argument list <A> is compared to the template parameter list of … Read more

Differences between std::make_unique and std::unique_ptr with new

The motivation behind make_unique is primarily two-fold: make_unique is safe for creating temporaries, whereas with explicit use of new you have to remember the rule about not using unnamed temporaries. foo(make_unique<T>(), make_unique<U>()); // exception safe foo(unique_ptr<T>(new T()), unique_ptr<U>(new U())); // unsafe* The addition of make_unique finally means we can tell people to ‘never’ use new … Read more

Can modern C++ get you performance for free?

I am aware of 5 general categories where recompiling a C++03 compiler as C++11 can cause unbounded performance increases that are practically unrelated to quality of implementation. These are all variations of move semantics. std::vector reallocate struct bar{ std::vector<int> data; }; std::vector<bar> foo(1); foo.back().data.push_back(3); foo.reserve(10); // two allocations and a delete occur in C++03 every … Read more

Lambda capture as const reference?

In c++14 using static_cast / const_cast: [&best_string = static_cast<const std::string&>(best_string)](const string& s) { best_string = s; // fails }; DEMO In c++17 using std::as_const: [&best_string = std::as_const(best_string)](const string& s) { best_string = s; // fails }; DEMO 2

How to implement classic sorting algorithms in modern C++?

Algorithmic building blocks We begin by assembling the algorithmic building blocks from the Standard Library: #include <algorithm> // min_element, iter_swap, // upper_bound, rotate, // partition, // inplace_merge, // make_heap, sort_heap, push_heap, pop_heap, // is_heap, is_sorted #include <cassert> // assert #include <functional> // less #include <iterator> // distance, begin, end, next the iterator tools such as … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)