Use include <cstring> and std::memcpy etc
This commit is contained in:
33
style.md
33
style.md
@@ -22,6 +22,39 @@ This document describes the C++ coding style used in the WeaselDB project. These
|
||||
- Use modern C++ features: RAII, move semantics, constexpr, concepts where appropriate
|
||||
- Prefer standard library containers and algorithms over custom implementations
|
||||
|
||||
### C Library Functions and Headers
|
||||
- **Always use std:: prefixed versions** of C library functions for consistency and clarity
|
||||
- **Use C++ style headers** (`<cstring>`, `<cstdlib>`, etc.) instead of C style headers (`<string.h>`, `<stdlib.h>`, etc.)
|
||||
- This applies to all standard libc functions: `std::abort()`, `std::fprintf()`, `std::free()`, `std::memcpy()`, `std::strlen()`, `std::strncpy()`, `std::memset()`, `std::signal()`, etc.
|
||||
- Exception: Functions with no std:: equivalent (e.g., `perror()`, `gai_strerror()`) and system-specific headers (e.g., `<unistd.h>`, `<fcntl.h>`)
|
||||
```cpp
|
||||
// Preferred - C++ style
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <csignal>
|
||||
std::abort();
|
||||
std::fprintf(stderr, "Error message\n");
|
||||
std::free(ptr);
|
||||
std::memcpy(dest, src, size);
|
||||
std::strlen(str);
|
||||
std::strncpy(dest, src, n);
|
||||
std::memset(ptr, value, size);
|
||||
std::signal(SIGTERM, handler);
|
||||
|
||||
// Avoid - C style
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
abort();
|
||||
fprintf(stderr, "Error message\n");
|
||||
free(ptr);
|
||||
memcpy(dest, src, size);
|
||||
strlen(str);
|
||||
strncpy(dest, src, n);
|
||||
memset(ptr, value, size);
|
||||
signal(SIGTERM, handler);
|
||||
```
|
||||
|
||||
### Data Types
|
||||
- **Almost always signed** - prefer `int`, `int64_t`, `size_t` over unsigned types except for:
|
||||
- Bit manipulation operations
|
||||
|
||||
Reference in New Issue
Block a user