-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path.cpp_rules
More file actions
68 lines (51 loc) · 2.32 KB
/
Copy path.cpp_rules
File metadata and controls
68 lines (51 loc) · 2.32 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
# C/C++ Development Rules
This file contains coding guidelines and best practices for C/C++ development in this project.
## Code Style
- Follow modern C++ standards (C++20/23 when possible)
- Use clang-format for consistent formatting
- Maximum line length: 100 characters
- Use meaningful variable and function names
- Prefer explicit over implicit
## Memory Safety
- **Always use RAII patterns** for resource management
- Avoid manual `new`/`delete` - use smart pointers instead
- Use `std::unique_ptr` for exclusive ownership
- Use `std::shared_ptr` for shared ownership
- Use `std::weak_ptr` to break circular references
- Prefer stack allocation over heap when possible
## Modern C++ Features
- Use `nullptr` instead of `NULL` or `0`
- Use `auto` for type deduction when it improves readability
- Use range-based for loops: `for (const auto& item : container)`
- Use `constexpr` for compile-time computation
- Leverage move semantics with `std::move` for large objects
- Use structured bindings: `auto [key, value] = map.find(...)`
## Containers and Algorithms
- Prefer STL containers over C-style arrays
- Use `std::array` for fixed-size arrays
- Use `std::vector` for dynamic arrays
- Use STL algorithms (`std::find`, `std::transform`, etc.) over manual loops
- Use `std::string_view` for non-owning string references
## Error Handling
- Use exceptions for exceptional circumstances
- Use `std::optional` for values that may not exist
- Use `std::expected` (C++23) or custom Result types for operations that may fail
- Document which exceptions a function may throw
## Performance
- Profile before optimizing
- Use `const` and `constexpr` liberally
- Pass large objects by const reference
- Return large objects by value (rely on RVO/NRVO)
- Use `std::move` when transferring ownership
- Consider `noexcept` for functions that don't throw
## Threading and Concurrency
- Use `std::thread`, `std::async`, or thread pools
- Protect shared data with `std::mutex` or `std::shared_mutex`
- Use `std::lock_guard` or `std::scoped_lock` for RAII mutex locking
- Prefer `std::atomic` for simple atomic operations
- Use `std::condition_variable` for thread synchronization
## Build System
- Use CMake as the build system
- Organize code into logical libraries/modules
- Enable all warnings: `-Wall -Wextra -Wpedantic`
- Treat warnings as errors in CI: `-Werror`