I’m switching to GCC 4.6.1, and it starts to complain about code which works fine with GCC 4.4 and MSVC10. It seems that it doesn’t want to convert between shared_ptr and bool when returning from a function like this:
class Class { shared_ptr<Somewhere> pointer_; };
bool Class::Function () const
{
return pointer_;
}
using
return static_cast<bool> (pointer_);
everything works. What the heck is going on? This is with --std=cpp0x.
In C++11,
shared_ptrhas anexplicitoperator boolwhich means that ashared_ptrcan’t be implicitly converted to abool.This is to prevent some potentially pitfalls where a
shared_ptrmight accidentally be converted in arithmetic expressions and the similar situations.Adding an explicit cast is a valid fix to your code.
You could also do
return pointer_.get() != 0;,return pointer_.get();or evenreturn pointer_ != nullptr;.