[bind10-dev] suggested team call agenda
Francis Dupont
fdupont at isc.org
Tue Dec 11 12:00:52 UTC 2012
> Actually, I used:
> if (pointer != shared_ptr<Type>()) {
> in the test and it was pointed out in a review this can be shortened.
=> if the smart pointer has a boolean operator this is useless.
> My view is, pointer either points to something or does not point to anything.
=> this is why recent C++ has nullptr.
> That is very intuitive meaning in a conditional statement, that is generally
> used in many projects and that's why each smart pointer has a bool operator
> so it can be checked that way.
=> this is IMHO the best way: what I am against is an implicit cast
(and in general any implicit casts).
> I believe it is more readable than using
> if (pointer != NULL)
=> this is C style so not the best in C++ (:-)!
> and that the readability should be the primary criterion about style (with
> obviously avoiding some pitfalls of language).
=> I disagree: IMHO type safety is more important but perhaps I used
too long typed functional languages where types are infered so programs
are both well typed and concise (same for memory management...).
> And these together make me want to know the reason why it should be
> considered bad style in the first place
=> it is bad style to have implicit casts so either:
- there is no "operator <bool-type>() const" and it is bad style
- there is an "operator <bool-type>() const" and it is good style
> and if we want to either change that or make it
> consistent with the shared pointers somehow.
=> shared_ptr Boost doc says (I didn't check the equivalent in last C++ docs):
*conversions*
operator unspecified-bool-type () const; // never throws
Returns: an unspecified value that, when used in boolean contexts, is
equivalent to get() != 0.
Throws: nothing.
Notes: This conversion operator allows shared_ptr objects to be used
in boolean contexts, like if (p && p->valid()) {}. The actual target
type is typically a pointer to a member function, avoiding many of the
implicit conversion pitfalls.
[The conversion to bool is not merely syntactic sugar. It allows
shared_ptrs to be declared in conditions when using
dynamic_pointer_cast or weak_ptr::lock.]
Regards
Francis Dupont <fdupont at isc.org>
PS: in conclusion the recommendation to allow "if (pointer)" only for
smart pointers is likely to be the right one.
More information about the bind10-dev
mailing list