I was reading Effective C++ 3rd Edition. In page 70, the author says:
Like virtually all smart pointer classes,
tr1::shared_ptr
andauto_ptr
also overload the pointer dereferencing operators (operator->
andoperator*
), and this allows implicit conversion to the underlying raw pointers (...)
He then shows an example with shared_ptr
(which was part of tr1
at the time) featuring implicit conversion based on a class named Investment
:
shared_ptr<Investment> pi1();
bool taxable1 = !(pi1->isTaxFree());
^implicit conversion
shared_ptr<Investment> pi2();
bool taxable2 = !((*pi2).isTaxFree());
^implicit conversion
Well, I have since then written a few test cases with unique_ptr
and they hold up.
I also found out about unique_ptr
supporting arrays and shared_ptr
also going to (see note). However, in my testing, implicit conversion does not seem to work for smart pointers around arrays.
Example: I wanted this to be valid...
unique_ptr<int[]> test(new int[1]);
(*test)[0] = 5;
but it is not, according to my compiler (Visual C++ 2015 Update 3).
Then, from a little research, I found some evidence suggesting that implicit conversion isn't supported at all... like this one for instance: https://herbsutter.com/2012/06/21/reader-qa-why-dont-modern-smart-pointers-implicitly-convert-to.
At this point I am in doubt. Is it supported (by the Standard), or is it not?
Note: The book might be a bit outdated on this topic, since the author also says on page 65 that "there is nothing like auto_ptr
or tr1::shared_ptr
for dinamically allocated arrays, not even in TR1".