Why can a T* be passed in register, but a unique_ptr cannot?

Is this actually an ABI requirement, or maybe it’s just some pessimization in certain scenarios? One example is System V Application Binary Interface AMD64 Architecture Processor Supplement. This ABI is for 64-bit x86-compatible CPUs (Linux x86_64 architecure). It is followed on Solaris, Linux, FreeBSD, macOS, Windows Subsystem for Linux: If a C++ object has either … Read more

Exception safety and make_unique

Not only when you have multiple allocations, but whenever you can throw at different places. Consider this: f(make_unique<T>(), function_that_can_throw()); Versus: f(unique_ptr<T>(new T), function_that_can_throw()); In the second case, the compiler is allowed to call (in order): new T function_that_can_throw() unique_ptr<T>(…) Obviously if function_that_can_throw actually throws then you leak. make_unique prevents this case. And of course, a … Read more

Proper way to create unique_ptr that holds an allocated array

Using the T[] specialisation: std::unique_ptr<unsigned char[]> testData(new unsigned char[16000]()); Note that, in an ideal world, you would not have to explicitly use new to instantiate a unique_ptr, avoiding a potential exception safety pitfall. To this end, C++14 provides you with the std::make_unique function template. See this excellent GOTW for more details. The syntax is: auto … Read more

How do I use unique_ptr for pimpl?

I believe that your test_help.cpp actually sees the ~Help() destructor that you declared default. In that destructor, the compiler tries to generate the unique_ptr destructor, too, but it needs the Impl declaration for that. So if you move the destructor definition to the Help.cpp, this problem should be gone. — EDIT — You can define … Read more

Forward declaration with unique_ptr? [duplicate]

It’s explicitly legal. The rule is that the types used to instantiate a template in the standard library must be complete, unless otherwise specified. In the case of unique_ptr, ยง20.7.1/5 says โ€œ[…] The template parameter T of unique_ptr may be an incomplete type.โ€ There are certain operations on the pointer which require a complete type; … Read more

How to implement make_unique function in C++11? [duplicate]

Version by Stephan T. Lavavej (also known by STL) who originally proposed adding this function to C++14 #include <cstddef> #include <memory> #include <type_traits> #include <utility> namespace std { template<class T> struct _Unique_if { typedef unique_ptr<T> _Single_object; }; template<class T> struct _Unique_if<T[]> { typedef unique_ptr<T[]> _Unknown_bound; }; template<class T, size_t N> struct _Unique_if<T[N]> { typedef void … Read more

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