aboutsummaryrefslogtreecommitdiff
path: root/docs/cpp-coding/09-Considering_Correctness.md
blob: 5bc8b61ec41f56e31dbd932523eadb93204dd2ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Considering Correctness

## Avoid Typeless Interfaces


Bad Idea:

```cpp
std::string find_file(const std::string &base, const std::string &pattern);
```

Better Idea:

```cpp
std::filesystem::path find_file(const std::filesystem::path &base, const std::regex &pattern);
```

The above is better but still suffers from having implicit conversions from `std::string` to `std::filesystem::path` and back.

Consider using a typesafe library like

 * https://foonathan.net/type_safe/
 * https://github.com/rollbear/strong_type
 * https://github.com/joboccara/NamedType

Note that stronger typing can also allow for more compiler optimizations.

* [Sorting in C vs C++](Sorting in C vs C++.pdf)