Project

General

Profile

Actions

Bug #3368

closed

Code style rule 3.11 is wrong

Added by Davide Pesavento almost 9 years ago. Updated almost 9 years ago.

Status:
Closed
Priority:
Low
Category:
Docs
Target version:
Start date:
Due date:
% Done:

100%

Estimated time:

Description

Rule 3.11 states the following:

When checking if shared_ptr points to an object, explicit static_cast<bool> must be used.
shared_ptr in C++11 (unlike boost::shared_ptr) does not have implicit conversion to bool.

This is simply wrong. The fact that operator bool() is declared explicit in std::shared_ptr does NOT matter in a boolean context. This is true practically every time a shared_ptr is checked for validity.

The idiomatic way is:

shared_ptr<T> ptr;
if (ptr) { ... }
// OR
if (ptr == nullptr) { ... }
// OR
if (ptr != nullptr) { ... }
// etc...

Moreover, if the implicit conversion isn't possible (when not in a boolean context), the compilation will fail, so there's really no reason why this has to be a code style rule.

Actions

Also available in: Atom PDF