Repository navigation
Expand file tree
/
Copy pathcpp.mdc
More file actions
70 lines (52 loc) · 1.78 KB
/
Copy pathcpp.mdc
File metadata and controls
70 lines (52 loc) · 1.78 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
---
description: C++ class layout, coding conventions, and best practices
globs: "**/*.{cpp,hpp,h,cc,cxx}"
alwaysApply: false
---
# C++ Coding Conventions
> Based on [Vinnie Falco's](https://github.com/vinniefalco) C++ rules from [cppalliance/coro-io-context](https://github.com/cppalliance/coro-io-context)
## Class Layout
- Private data members first
- Public interface last
- Destructor comes first in the public section
- Use `struct` if all members are public, otherwise use `class`
- Little to no ASCII-art dividers
## Encapsulation
- Never break encapsulation without explicit instruction
- Prefer composition over inheritance where appropriate
## Error Handling
- Don't call `make_error_code` directly unless necessary
- Idiomatic `boost::system::error_code` check is `ec.failed()`, not `ec` or `!!ec`
- For `std::error_code`, use `if (ec)` or `ec.value() != 0`
## Min/Max Protection
- Always protect `min`/`max` with parentheses: `(std::min)(a, b)`
- Include `<algorithm>` sparingly - it's costly
- Don't include `<algorithm>` just for `min`/`max`
## Testing
- Always update the tests after changing code
- Write unit tests for public headers in corresponding `.cpp` files
- Test edge cases and error conditions
## Example Class Layout
```cpp
class connection
{
// Private data members first
socket socket_;
buffer buffer_;
bool connected_ = false;
public:
// Destructor first
~connection();
// Constructors
connection();
connection(connection&&) noexcept;
connection& operator=(connection&&) noexcept;
// Deleted operations
connection(connection const&) = delete;
connection& operator=(connection const&) = delete;
// Public interface
void connect(endpoint const& ep);
void disconnect();
bool is_connected() const noexcept;
};
```