Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions bestpractices/c++practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,46 +257,44 @@ Below follow general guidelines with examples:
#### The use of `auto` is ok if the right-hand side makes clear which type it is

```c++
auto* xyz = new Xyz();
auto* xyz = new Xyz(); // OK: type is clear from calling the constructor
```

```c++
auto xyz = <whatever>_cast<Xyz>(...);
auto xyz = <whatever>_cast<Xyz>(...); // OK: type is clear from the cast
```

```c++
auto xyz = getAnything<Xyz>(...);
auto xyz = getAnything<Xyz>(...); // OK: type is clear from the context and type parameter
```

Counter examples:

```c++
auto value = randomThing.weirdProperty->getValue(); // non-obvious type
auto value = randomThing.weirdProperty->getValue(); // bad: non-obvious type
```

```c++
auto doStuff() { ... } // auto as return type
auto doStuff() { ... } // bad: auto as return type
```

```c++
auto xyz = 1; // unclear what type of integer
auto xyz = 1; // bad: unclear what type of integer
```

#### The use of `auto` is ok if the type is long or verbose

```c++
auto it = foo.begin(); // iterator
auto it = foo.begin(); // OK: iterator
```

```c++
auto lambda = [](){...};
auto lambda = [](){...}; // OK: function type
```

#### The use of `auto` is ok if redundancy is avoided

```c++
std::unordered_map<std::string, int> map;
for (const auto& [key, value] : map) { ... }
for (const auto& [key, value] : map) { ... } // OK: map declaration contains the type
```

## Main code path and indentation
Expand Down