I am having trouble understanding why the following copy-initialisation doesn't compile:A unique_ptr<derived> can be moved into a unique_ptr<base>, so why the second statement works and the last doesn't? Aren't non-explicit constructors considered when copy-initialising? The error from gcc-8.2.0 is
Why doesn't this work?Check out the incomprehensible compile error https://godbolt.org/z/Uhp9NO.The problem is that m calls the default constructor of std::unique_ptr<int, decltype(&deleter)>, which is not available because it requires the deleter pointer.
It seems an easy way to circumvent a unique_ptr is to use a pointer to the unique_ptr object. And that's not difficult. So using the unique_ptr is sort of a gentleman's agreement and not really super enforced?
Check following contrived program:From std::function constructor manual, I think constructing std::function object may throw exception, even the ratio is very low:
I want to store Particle objects in a vector object, so I can access it later. These particles (Electrons and Protons) are inherited from the Particle class which contains a toString() virtual method. This toString() method is then overrided in Electron and Proton classes.
Are there any advantages of using std::unique_ptr<T>& instead of std::unique_ptr<T>? For example, in function arguments?
This code:yields the following error (with GCC 6.3):Now, I can work around this, like so:but why do I have to? I mean, why isn't the compiler willing to "plug" the second template parameter of std::unique_ptr with its default value and allow std::unique_ptr to be used as a template argument to...
A simple codeproduce a compile error ("no suitable conversion"). I found similar question where the solution is to use std::move. I tried this
Could you tell me what is going on here? And why it is possible?I thought that make_unique returned an r-value...
Could you tell me what is going here? And why it is possible?I thought that return of make_unique is r-value...