Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't smart pointers have conversion operator back to the base pointer?

If often find myself using code like this:

boost::scoped_ptr<TFoo> f(new TFoo);

Bar(f.get());  // call legacy or 3rd party function : void Bar (TFoo *)

Now, I think the smart pointers could easily define an implicit conversion operator back to the 'raw' pointer type, which would allow this code to still be valid, and ease the 'smartening' of old code

Bar(f);

But, they don't -or at least, not the ones I've found. Why?

like image 266
Roddy Avatar asked Jul 29 '26 05:07

Roddy


1 Answers

IMO implicit conversion is the root of all evil in c++, and one the toughest kinds of bugs to track down.

It's good practice not to rely on them - you can't predict all behaviours.

like image 51
Nim Avatar answered Jul 30 '26 19:07

Nim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!