What should be the behavior of unique_ptr in this situation?

Say I have the following:

std::unique_ptr<A> pA; pA(new A); 

In this confusing example, what should be the behavior of pA(new A); ?

As far as I can tell, in MSVC2010 void operator()(T*) const; from default_delete is called immediately after returning new and immediately deletes the pointer. If g ++ (4.7.0) gave me an error no match for call (std::unique_ptr<A>)(A*) .

+4
source share
2 answers

The code should not compile. std::unique_ptr does not overload operator() .

Visual C ++ 2011 Developer Preview correctly rejects code. Visual C ++ 2010 accepts an error code in its implementation of std::unique_ptr .

+6
source

MSVC uses stateless determiner optimization for unique_ptr , i.e. uses optimization with an empty base class and simply inherits from the deleter. Unfortunately, inheritance is public , so you have access to the overloaded operator() default_delete functor.

+1
source

Source: https://habr.com/ru/post/1385805/


All Articles