Skip to content

Commit 8692ffe

Browse files
committed
refactoring
1 parent 27b8717 commit 8692ffe

3 files changed

Lines changed: 18 additions & 22 deletions

File tree

‎src/const-cast.cpp‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,40 @@ namespace {
1212

1313
// const_cast
1414
// - const_cast is the only cast that can add/remove const (or volatile)
15-
// from a type.
15+
// from a type — nothing else
1616
// - main legitimate use: interop with legacy C APIs lacking const correctness
1717
// - if you need const_cast a lot — fix the API instead
1818
//
1919
// Key notes:
20-
// - only cast that adds/removes const and volatile — nothing else
2120
// - removing const is safe ONLY if original object was not declared const
2221
// - modifying a truly const object after const_cast = UB
2322
// - prefer mutable over const_cast for class members
2423

2524

2625
//// remove const — call legacy C API that isn't const-correct
27-
void c_api(char* s) { // old C API — doesn't modify s but not const
28-
printf("%s", s);
26+
void c_api(char* ch) { // old C API — doesn't modify ch but not const
27+
printf("%s", ch);
2928
}
3029

3130
TEST_CASE("con-cas-1") {
3231

3332
const char* msg = "hello";
33+
// c_api(const_cast<char*>(msg)); // ✓ safe — c_api doesn't modify it
3434
c_api(const_cast<char*>(msg)); // ✓ safe — c_api doesn't modify it
35+
// remove constness and cast msg to char*
3536

3637

3738
//// remove const — only safe if original object was not const
3839
const int* ptr = new int(42); // pointer to const int, !! but underlying
3940
// object is still not const !!
40-
int* mp = const_cast<int*>(ptr);
41-
*mp = 99; // ✓ safe — original object was non-const
41+
int* mut_ptr = const_cast<int*>(ptr);
42+
*mut_ptr = 99; // ✓ safe — original object was non-const
4243
REQUIRE(*ptr == 99);
4344
delete ptr;
4445

4546
const int x = 42; // !! underlying object is const !!
46-
int* mut_ptr = const_cast<int*>(&x);
47-
*mut_ptr = 99; // ❌ UB — original object IS const
47+
int* mut_ptr_2 = const_cast<int*>(&x);
48+
*mut_ptr_2 = 99; // ❌ UB — original object IS "truly" const
4849
REQUIRE(x == 42);
4950

5051

@@ -65,8 +66,6 @@ TEST_CASE("con-cas-1") {
6566
}
6667
};
6768

68-
69-
7069
}
7170

7271
}

‎src/local-static-objs.cpp‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,18 @@ namespace {
2020
// across TUs: order of destruction matches reverse of construction order ✓
2121
//
2222
//// Key notes:
23-
// - initialized on first call — not at program start (lazy)
24-
// - destroyed at program exit — reverse construction order
25-
// - thread-safe initialization since C++11 — no manual mutex needed
2623
// - combine with constinit (C++20) for compile-time init guarantee
2724
// - static local vs static member: local = lazy; member = eager at startup
28-
// - singleton via local static is the safest C++ singleton pattern
2925

3026

3127
//// singleton pattern — most common use
28+
// - singleton via local static is the safest C++ singleton pattern
3229
class Logger {
3330
public:
3431
static Logger& instance() {
35-
static Logger log; // created once on first call ✓
32+
static Logger log;
33+
// created on first call — not at program start (lazy)
34+
// destroyed at program exit — reverse construction order
3635
return log; // thread-safe since C++11
3736
}
3837
private:
@@ -43,11 +42,10 @@ TEST_CASE("loc-sta-1") {
4342
Logger logger = Logger::instance();
4443
}
4544

46-
4745
TEST_CASE("loc-sta-2") {
4846

4947
struct Foo {};
50-
//// thread safety — C++11 guarantees
48+
//// - thread-safe initialization since C++11 — no manual mutex needed
5149
static Foo obj; // if two threads call simultaneously:
5250
// one initializes, other waits until ctor ends — no race ✓
5351
// guaranteed by standard since C++11

‎src/variadic-templates-cpp11.cpp‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,22 @@ using namespace std;
1616
// - the foundation of std::tuple, std::make_shared, and perfect forwarding.
1717
//
1818
// Key notes:
19-
// - typename... Args = parameter pack (types)
20-
// - Args... args = pack expansion
21-
// - sizeof...(Args) = count at compile time
22-
// - ... placement matters — Args... expands the pack
2319
// - C++11: recursion to process pack; C++17: fold expressions (prefer)
2420
// - foundation of: tuple, make_shared, make_unique, std::invoke, std::apply
2521

2622

2723
//// basic — accept any number/type of args
24+
// - typename... Args = parameter pack (types)
25+
// - Args... args = pack expansion
26+
// - ... placement matters — Args... expands the pack
2827
template<typename... Args>
2928
void print(Args... args) {
3029
(std::cout << ... << args); // fold expression (C++17) ✓
3130
}
3231
// print(1, " ", 3.14, " ", "hi"); // ✓ any types, any count
3332

3433

35-
//// sizeof... — count params
34+
//// - sizeof...(Args) = count at compile time
3635
template<typename... Args>
3736
void countArgs(Args... args) {
3837
std::cout << sizeof...(args); // number of args ✓

0 commit comments

Comments
 (0)