Why do C++ libraries and frameworks never use smart pointers?

Apart from the fact that many libraries were written before the advent of standard smart pointers, the biggest reason is probably the lack of a standard C++ Application Binary Interface (ABI). If you’re writing a header-only library, you can pass around smart pointers and standard containers to your heart’s content. Their source is available to … 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

Why would I std::move an std::shared_ptr?

I think that the one thing the other answers did not emphasize enough is the point of speed. std::shared_ptr reference count is atomic. increasing or decreasing the reference count requires atomic increment or decrement. This is hundred times slower than non-atomic increment/decrement, not to mention that if we increment and decrement the same counter we … Read more

std::auto_ptr to std::unique_ptr

You cannot do a global find/replace because you can copy an auto_ptr (with known consequences), but a unique_ptr can only be moved. Anything that looks like std::auto_ptr<int> p(new int); std::auto_ptr<int> p2 = p; will have to become at least like this std::unique_ptr<int> p(new int); std::unique_ptr<int> p2 = std::move(p); As for other differences, unique_ptr can handle … Read more

RAII and smart pointers in C++

A simple (and perhaps overused) example of RAII is a File class. Without RAII, the code might look something like this: File file(“/path/to/file”); // Do stuff with file file.close(); In other words, we must make sure that we close the file once we’ve finished with it. This has two drawbacks – firstly, wherever we use … Read more

smart pointers (boost) explained

Basic properties of smart pointers It’s easy when you have properties that you can assign each smart pointer. There are three important properties. no ownership at all transfer of ownership share of ownership The first means that a smart pointer cannot delete the object, because it doesn’t own it. The second means that only one … Read more

Why can I not push_back a unique_ptr into a vector?

You need to move the unique_ptr: vec.push_back(std::move(ptr2x)); unique_ptr guarantees that a single unique_ptr container has ownership of the held pointer. This means that you can’t make copies of a unique_ptr (because then two unique_ptrs would have ownership), so you can only move it. Note, however, that your current use of unique_ptr is incorrect. You cannot … Read more

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