Skip to content

Commit 46ea597

Browse files
committed
🎚️ Switch to the standard library style for C++.
Switching away from the "Python" style.
1 parent 924c391 commit 46ea597

File tree

14 files changed

+334
-370
lines changed

14 files changed

+334
-370
lines changed

cpp/calculator/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,5 @@ project(Calculator)
33

44
set(CMAKE_CXX_STANDARD 20)
55

6-
find_package(fmt CONFIG REQUIRED)
7-
86
file(GLOB FILES "*.cpp")
97
add_executable(Calculator ${FILES})
10-
11-
target_link_libraries(Calculator PRIVATE fmt::fmt)

cpp/calculator/calculator.cpp

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,84 @@
22

33
#include "calculator.hpp"
44

5-
#include <fmt/core.h>
5+
#include <string>
66

77
using namespace std;
88

9-
shared_ptr<Expr> Expr::operator+(const shared_ptr<Expr>& other) const {
10-
return make_shared<Eval>(shared_from_this(), other, "+",
11-
[](int a, int b) { return a + b; });
9+
shared_ptr<expression> expression::operator+(
10+
const shared_ptr<expression>& other) const {
11+
return make_shared<evaluation>(shared_from_this(), other, "+",
12+
[](int a, int b) { return a + b; });
1213
}
1314

14-
shared_ptr<Expr> Expr::operator-(const shared_ptr<Expr>& other) const {
15-
return make_shared<Eval>(shared_from_this(), other, "-",
16-
[](int a, int b) { return a - b; });
15+
shared_ptr<expression> expression::operator-(
16+
const shared_ptr<expression>& other) const {
17+
return make_shared<evaluation>(shared_from_this(), other, "-",
18+
[](int a, int b) { return a - b; });
1719
}
1820

19-
shared_ptr<Expr> Expr::operator*(const shared_ptr<Expr>& other) const {
20-
return make_shared<Eval>(shared_from_this(), other, "*",
21-
[](int a, int b) { return a * b; });
21+
shared_ptr<expression> expression::operator*(
22+
const shared_ptr<expression>& other) const {
23+
return make_shared<evaluation>(shared_from_this(), other, "*",
24+
[](int a, int b) { return a * b; });
2225
}
2326

24-
shared_ptr<Expr> Expr::operator/(const shared_ptr<Expr>& other) const {
25-
return make_shared<Eval>(shared_from_this(), other, "/",
26-
[](int a, int b) { return a / b; });
27+
shared_ptr<expression> expression::operator/(
28+
const shared_ptr<expression>& other) const {
29+
return make_shared<evaluation>(shared_from_this(), other, "/",
30+
[](int a, int b) { return a / b; });
2731
}
2832

29-
Expr::~Expr() {}
33+
expression::~expression() {}
3034

31-
Lit::Lit(int data) : data_(data) {}
35+
literal::literal(int data) : data_(data) {}
3236

33-
shared_ptr<const Expr> Lit::simplify() const {
37+
shared_ptr<const expression> literal::simplify() const {
3438
return shared_from_this();
3539
}
3640

37-
string Lit::to_string() const {
38-
return fmt::format("{}", data_);
41+
literal::operator string() const {
42+
return to_string(data_);
3943
}
4044

41-
int Lit::data() const {
45+
int literal::data() const {
4246
return data_;
4347
}
4448

45-
Lit::~Lit() {}
49+
literal::~literal() {}
4650

47-
Var::Var(string data) : data_(data) {}
51+
variable::variable(string data) : data_(data) {}
4852

49-
shared_ptr<const Expr> Var::simplify() const {
53+
shared_ptr<const expression> variable::simplify() const {
5054
return shared_from_this();
5155
}
5256

53-
string Var::to_string() const {
57+
variable::operator string() const {
5458
return data_;
5559
}
5660

57-
Var::~Var() {}
61+
variable::~variable() {}
5862

59-
Eval::Eval(shared_ptr<const Expr> left,
60-
shared_ptr<const Expr> right,
61-
string token,
62-
function<int(int, int)> func)
63+
evaluation::evaluation(shared_ptr<const expression> left,
64+
shared_ptr<const expression> right,
65+
string token,
66+
function<int(int, int)> func)
6367
: left_(left), right_(right), token_(token), func_(func) {}
6468

65-
shared_ptr<const Expr> Eval::simplify() const {
66-
auto lp = dynamic_pointer_cast<const Lit>(left_->simplify());
67-
auto rp = dynamic_pointer_cast<const Lit>(right_->simplify());
69+
shared_ptr<const expression> evaluation::simplify() const {
70+
auto lp = dynamic_pointer_cast<const literal>(left_->simplify());
71+
auto rp = dynamic_pointer_cast<const literal>(right_->simplify());
6872

6973
// Do nothing because no simplification is needed.
7074
if (lp == nullptr || rp == nullptr) {
7175
return shared_from_this();
7276
}
7377

74-
return make_shared<Lit>(func_(lp->data(), rp->data()));
78+
return make_shared<literal>(func_(lp->data(), rp->data()));
7579
}
7680

77-
string Eval::to_string() const {
78-
return fmt::format("({} {} {})", left_->to_string(), token_,
79-
right_->to_string());
81+
evaluation::operator string() const {
82+
return string(*left_) + " " + token_ + " " + string(*right_);
8083
}
8184

82-
Eval::~Eval() {}
85+
evaluation::~evaluation() {}

cpp/calculator/calculator.hpp

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,63 +9,63 @@
99
using namespace std;
1010

1111
/// API: Lit, Var create two pointer to expressions.
12-
/// that has simplify(), to_string(), Add, Sub, Mul as methods.
12+
/// that has simplify(), operator string(), Add, Sub, Mul as methods.
1313

14-
class Expr : public enable_shared_from_this<Expr> {
14+
class expression : public enable_shared_from_this<expression> {
1515
public:
16-
shared_ptr<Expr> operator+(const shared_ptr<Expr>& other) const;
17-
shared_ptr<Expr> operator-(const shared_ptr<Expr>& other) const;
18-
shared_ptr<Expr> operator*(const shared_ptr<Expr>& other) const;
19-
shared_ptr<Expr> operator/(const shared_ptr<Expr>& other) const;
16+
shared_ptr<expression> operator+(const shared_ptr<expression>& other) const;
17+
shared_ptr<expression> operator-(const shared_ptr<expression>& other) const;
18+
shared_ptr<expression> operator*(const shared_ptr<expression>& other) const;
19+
shared_ptr<expression> operator/(const shared_ptr<expression>& other) const;
2020

21-
virtual shared_ptr<const Expr> simplify() const = 0;
22-
virtual string to_string() const = 0;
21+
virtual shared_ptr<const expression> simplify() const = 0;
22+
virtual operator string() const = 0;
2323

24-
virtual ~Expr();
24+
virtual ~expression();
2525
};
2626

27-
class Lit : public Expr {
27+
class literal : public expression {
2828
public:
29-
Lit(int data);
29+
literal(int data);
3030

31-
string to_string() const override;
32-
shared_ptr<const Expr> simplify() const override;
31+
operator string() const override;
32+
shared_ptr<const expression> simplify() const override;
3333

3434
int data() const;
3535

36-
~Lit() override;
36+
~literal() override;
3737

3838
private:
3939
int data_;
4040
};
4141

42-
class Var : public Expr {
42+
class variable : public expression {
4343
public:
44-
Var(string data);
44+
variable(string data);
4545

46-
string to_string() const override;
47-
shared_ptr<const Expr> simplify() const override;
46+
operator string() const override;
47+
shared_ptr<const expression> simplify() const override;
4848

49-
~Var() override;
49+
~variable() override;
5050

5151
private:
5252
string data_;
5353
};
5454

55-
class Eval : public Expr {
55+
class evaluation : public expression {
5656
public:
57-
Eval(shared_ptr<const Expr> left,
58-
shared_ptr<const Expr> right,
59-
string token,
60-
function<int(int, int)> func);
61-
~Eval() override;
57+
evaluation(shared_ptr<const expression> left,
58+
shared_ptr<const expression> right,
59+
string token,
60+
function<int(int, int)> func);
61+
~evaluation() override;
6262

63-
string to_string() const override;
64-
shared_ptr<const Expr> simplify() const override;
63+
operator string() const override;
64+
shared_ptr<const expression> simplify() const override;
6565

6666
private:
67-
shared_ptr<const Expr> left_;
68-
shared_ptr<const Expr> right_;
67+
shared_ptr<const expression> left_;
68+
shared_ptr<const expression> right_;
6969
string token_;
7070
function<int(int, int)> func_;
7171
};

cpp/casting.cpp

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22

33
#include <iostream>
44

5-
class Printable {
5+
class printer {
66
public:
77
virtual void print() const = 0;
88
};
99

10-
class CustomInt;
11-
class CustomFloat;
10+
class custom_int;
11+
class custom_float;
1212

13-
class CustomInt : public Printable {
13+
class custom_int : public printer {
1414
public:
15-
CustomInt();
16-
CustomInt(int number);
15+
custom_int();
16+
custom_int(int number);
1717

1818
operator int() const;
1919
operator float() const;
20-
operator CustomFloat() const;
20+
operator custom_float() const;
2121

2222
void print() const override {
2323
std::cout << "CustomInt: " << value_ << std::endl;
@@ -27,14 +27,14 @@ class CustomInt : public Printable {
2727
int value_;
2828
};
2929

30-
class CustomFloat : public Printable {
30+
class custom_float : public printer {
3131
public:
32-
CustomFloat();
33-
CustomFloat(float a);
32+
custom_float();
33+
custom_float(float a);
3434

3535
operator int() const;
3636
operator float() const;
37-
operator CustomInt() const;
37+
operator custom_int() const;
3838

3939
void print() const override {
4040
std::cout << "CustomFloat: " << value_ << std::endl;
@@ -46,41 +46,41 @@ class CustomFloat : public Printable {
4646

4747
// Implementations
4848

49-
CustomInt::CustomInt() : value_(0) {}
50-
CustomInt::CustomInt(int number) : value_(number) {}
51-
CustomInt::operator int() const {
49+
custom_int::custom_int() : value_(0) {}
50+
custom_int::custom_int(int number) : value_(number) {}
51+
custom_int::operator int() const {
5252
return value_;
5353
}
54-
CustomInt::operator float() const {
54+
custom_int::operator float() const {
5555
return static_cast<float>(value_);
5656
}
57-
CustomInt::operator CustomFloat() const {
58-
return CustomFloat(static_cast<float>(value_));
57+
custom_int::operator custom_float() const {
58+
return custom_float(static_cast<float>(value_));
5959
}
6060

61-
CustomFloat::CustomFloat() : value_(0.0f) {}
62-
CustomFloat::CustomFloat(float a) : value_(a) {}
63-
CustomFloat::operator int() const {
61+
custom_float::custom_float() : value_(0.0f) {}
62+
custom_float::custom_float(float a) : value_(a) {}
63+
custom_float::operator int() const {
6464
return static_cast<int>(value_);
6565
}
66-
CustomFloat::operator float() const {
66+
custom_float::operator float() const {
6767
return value_;
6868
}
69-
CustomFloat::operator CustomInt() const {
70-
return CustomInt(static_cast<int>(value_));
69+
custom_float::operator custom_int() const {
70+
return custom_int(static_cast<int>(value_));
7171
}
7272

7373
int main() {
74-
CustomInt custom_int(5);
75-
CustomFloat custom_float(3.14f);
74+
custom_int custom_int(5);
75+
custom_float custom_float(3.14f);
7676

7777
custom_int.print();
7878
custom_float.print();
7979

80-
auto casted_float = static_cast<CustomFloat>(custom_int);
80+
auto casted_float = static_cast<class custom_float>(custom_int);
8181
casted_float.print();
8282

83-
auto casted_int = static_cast<CustomInt>(custom_float);
83+
auto casted_int = static_cast<class custom_int>(custom_float);
8484
casted_int.print();
8585

8686
return 0;

cpp/classes.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44

55
using namespace std;
66

7-
class SomeClass {
7+
class some_class {
88
public:
9-
SomeClass(int i) : member(i) {}
9+
some_class(int i) : member(i) {}
1010
int& alias = member;
1111
const int& const_alias = member;
1212
int member;
1313
};
1414

1515
int main() {
16-
auto sc = SomeClass{1};
16+
auto sc = some_class{1};
1717
cout << sc.member << ' ' << sc.alias << ' ' << sc.const_alias << '\n';
1818
sc.member = 2;
1919
cout << sc.member << ' ' << sc.alias << ' ' << sc.const_alias << '\n';

cpp/const.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
/// Copyright (c) RenChu Wang - All Rights Reserved
22

3-
class ConstMember {
3+
class const_member {
44
public:
5-
ConstMember(int x) : member_(x) {}
5+
const_member(int x) : member_(x) {}
66

77
private:
88
const int member_;
99
};
1010

11-
class NormalMember {
11+
class normal_member {
1212
public:
13-
NormalMember(int x) : member_(x) {}
13+
normal_member(int x) : member_(x) {}
14+
int member() const { return member_; }
1415

1516
private:
1617
int member_;
1718
};
1819

1920
int main() {
2021
// No problem doing this.
21-
NormalMember nm{3};
22-
nm = NormalMember{4};
22+
normal_member nm{3};
23+
nm = normal_member{4};
2324

24-
ConstMember cm{9};
25-
// ConstMember& ConstMember::operator=(ConstMember&&)’ is implicitly
25+
const_member cm{9};
26+
// const_member& const_member::operator=(const_member&&)’ is implicitly
2627
// deleted because the default definition would be ill-formed
2728
//
2829
// operator= is deleted because of const member.
2930
//
30-
// cm = ConstMember{100};
31+
// cm = const_member{100};
3132
}

0 commit comments

Comments
 (0)