For test purpose, I need to construct an object on non-zero memory. This could be done with:Since this is tedious and made multiple times, I'd like to provide a function returning a smart pointer to such a Type instance. I came up with the following, but I fear undefined behavior...
I was reading this article by Herb Sutter about passing smart pointers to functions. He doesn't mention std::weak_ptr and honestly I can't find a good scenario where passing such smart pointer is useful.
This is my allocation:And this is my deletion :Is the code above OK? I probably should just use smart pointers, but I don't know how the code will look like with them? So if you throw in a smart-pointer code it would be greatly appreciated. Those pointers are used locally...
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 commonly come across the need to create arrays or vectors of polymorphic objects. I'd usually prefer to use references, rather than smart pointers, to the base class because they tend to be simpler.
Both as int, why 0 but 1 can be converted to std::shared_ptr<T>?How the disability of conversion from 1 to std::shared_ptr<T> be checked when compiling?
Here is a code snippet which i want to get a tree structure with smart pointer.But i got c3646('parent': unknown override specifier) and c4430(missing type specifier - int assumed) in vs.Does anybody know what's going on and how do i fix it>?
Why and when do I need to supply my own deleter? Isn't keyword delete sufficient enough? If you use a smart pointer to manage a resource other than memory allocated by new, remember to pass a deleter.
I have a class Foo a factory returning a pointer to it: and, since I have been told by Herb that a plain function with no special claim on the lifetime of Foo should take plain naked pointers: