Skip to content

Commit cf82410

Browse files
committed
clang applied
1 parent 459fec5 commit cf82410

File tree

141 files changed

+26421
-28873
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+26421
-28873
lines changed

src/CRTP.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
//http://www.vishalchovatiya.com/7-advance-cpp-concepts-idiom-examples-you-should-know/
2-
int main(){}
1+
// http://www.vishalchovatiya.com/7-advance-cpp-concepts-idiom-examples-you-should-know/
2+
int main() {}

src/RAII.cpp

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
#include <iostream>
2-
#include <thread>
32
#include <mutex>
3+
#include <thread>
44

55
/*
6-
RAII (Resource Acquisition is Initialization) tries to solve the problems like these:
7-
Problems:
8-
1)Problem #1
9-
int array=new int[size];
10-
array goes out of scope without being deleted, memory leak
6+
RAII (Resource Acquisition is Initialization) tries to solve the problems like
7+
these: Problems: 1)Problem #1 int array=new int[size]; array goes out of scope
8+
without being deleted, memory leak
119
1210
2)Problem #2
1311
std::mutex global_mu;
@@ -23,24 +21,24 @@ std::thread t1(func1);
2321
t1 goes out of scope and it is joinable but std::terminte is called;
2422
2523
26-
RAII is when you acquire "resources" in constructor and release them in destructor;
24+
RAII is when you acquire "resources" in constructor and release them in
25+
destructor;
2726
2827
Whats is a resource?
2928
1) must be acquired before use
3029
2) in limited supply
3130
example:
3231
heap: you should use malloc befprehand, heap is also limited
33-
files: you should open files before use, the number of files on your mahine is limited
34-
socket:
35-
mutex:
32+
files: you should open files before use, the number of files on your mahine is
33+
limited socket: mutex:
3634
3735
Example of RAII from standard c++:
3836
std::vector, std::string, std::unique_ptr, std::lock_gaurd
3937
4038
Refs: https://medium.com/swlh/what-is-raii-e016d00269f9
4139
*/
4240

43-
std::mutex globalMutex;
41+
std::mutex globalMutex;
4442
void lockMutexBad(bool shouldThrow) {
4543
std::cout << "Locking mutex manually..." << std::endl;
4644
globalMutex.lock();
@@ -59,16 +57,14 @@ void lockMutexBad(bool shouldThrow) {
5957
}
6058

6159
void lockMutexBadExample() {
62-
try
63-
{
64-
//tis call will llock the mutex without unlocking that
65-
lockMutexBad(true);
66-
} catch (...)
67-
{
68-
std::cout << "Caught exception" << std::endl;
69-
}
70-
//this call will stuck in the deadlcok
71-
lockMutexBad(false);
60+
try {
61+
// tis call will llock the mutex without unlocking that
62+
lockMutexBad(true);
63+
} catch (...) {
64+
std::cout << "Caught exception" << std::endl;
65+
}
66+
// this call will stuck in the deadlcok
67+
lockMutexBad(false);
7268
}
7369

7470
void lockMutexGood(bool shouldThrow) {
@@ -83,21 +79,17 @@ void lockMutexGood(bool shouldThrow) {
8379
}
8480

8581
void lockMutexGoodExample() {
86-
try
87-
{
88-
lockMutexGood(true);
89-
} catch (...)
90-
{
91-
std::cout << "Caught exception" << std::endl;
92-
}
93-
lockMutexGood(false);
82+
try {
83+
lockMutexGood(true);
84+
} catch (...) {
85+
std::cout << "Caught exception" << std::endl;
86+
}
87+
lockMutexGood(false);
9488
}
9589

90+
int main() {
91+
std::thread t1(lockMutexBadExample);
9692

97-
int main()
98-
{
99-
std::thread t1(lockMutexBadExample);
100-
101-
t1.join();
102-
return 0;
93+
t1.join();
94+
return 0;
10395
}

src/RTTI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
int main(){}
1+
int main() {}

src/RVO_NRVO_copy_elision.cpp

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ Then you will get
1616
"copy constructor"
1717
1818
=================Copy elision (Copy omission) ====================
19-
it is a compiler optimization technique that avoids unnecessary copying of objects.
19+
it is a compiler optimization technique that avoids unnecessary copying of
20+
objects.
2021
2122
This line:
2223
string s="hello";
@@ -28,35 +29,29 @@ should be broken down by the compiler as
2829
However, most of the C++ compilers avoid such overheads of creating a temporary
2930
object and then copying it.
3031
*/
31-
struct S
32-
{
33-
S() {std::cout<<"constructor" <<std::endl;}
32+
struct S {
33+
S() { std::cout << "constructor" << std::endl; }
3434

35-
S(S const &rhs) {std::cout<<"copy constructor" <<std::endl;}
35+
S(S const &rhs) { std::cout << "copy constructor" << std::endl; }
3636
};
3737

38-
//Return Value Optimization
39-
S RVO()
40-
{
41-
return S();
42-
}
38+
// Return Value Optimization
39+
S RVO() { return S(); }
4340

44-
//Named Return Value Optimization
45-
S NRVO()
46-
{ S s;
47-
return s;
41+
// Named Return Value Optimization
42+
S NRVO() {
43+
S s;
44+
return s;
4845
}
4946

50-
struct string
51-
{
52-
string(const char* str="\0") {std::cout<<"constructor" <<std::endl;}
53-
string (const string &rhs ){std::cout<<"copy constructor" <<std::endl;}
47+
struct string {
48+
string(const char *str = "\0") { std::cout << "constructor" << std::endl; }
49+
string(const string &rhs) { std::cout << "copy constructor" << std::endl; }
5450
};
5551

56-
int main()
57-
{
58-
S s1=RVO();
59-
S s2=NRVO();
52+
int main() {
53+
S s1 = RVO();
54+
S s2 = NRVO();
6055

61-
string s="hello";
56+
string s = "hello";
6257
}

src/SFINAE.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
int main(){}
1+
int main() {}

src/VTABLE_and_VPTR.cpp

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,31 @@
11
#include <iostream>
22

3-
4-
class base
5-
{
3+
class base {
64
public:
5+
void virtual foo() { std::cout << "foo from base" << std::endl; }
76

8-
void virtual foo()
9-
{
10-
std::cout<<"foo from base" <<std::endl;
11-
}
12-
13-
void virtual bar()
14-
{
15-
std::cout<<"bar from base" <<std::endl;
16-
}
17-
18-
void print()
19-
{
20-
std::cout<<"print from base" <<std::endl;
21-
}
7+
void virtual bar() { std::cout << "bar from base" << std::endl; }
228

9+
void print() { std::cout << "print from base" << std::endl; }
2310
};
2411

25-
26-
class derived : public base
27-
{
12+
class derived : public base {
2813
public:
29-
30-
void foo() override
31-
{
32-
std::cout<<"foo from derived" <<std::endl;
33-
}
34-
void print()
35-
{
36-
std::cout<<"print from derived" <<std::endl;
37-
}
14+
void foo() override { std::cout << "foo from derived" << std::endl; }
15+
void print() { std::cout << "print from derived" << std::endl; }
3816
};
3917

18+
int main() {
19+
base *bptr;
20+
derived derived_object;
21+
bptr = &derived_object;
4022

41-
int main()
42-
{
43-
base *bptr;
44-
derived derived_object;
45-
bptr = &derived_object;
46-
47-
//virtual function, binded at runtime, we got the foo() from derived
48-
bptr->foo();
49-
50-
// Non-virtual function, binded at compile time, we get print() from base
51-
bptr->print();
23+
// virtual function, binded at runtime, we got the foo() from derived
24+
bptr->foo();
5225

53-
// shadow method,
54-
derived_object.print();
26+
// Non-virtual function, binded at compile time, we get print() from base
27+
bptr->print();
5528

29+
// shadow method,
30+
derived_object.print();
5631
}

src/add.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
extern "C"
2-
{
1+
extern "C" {
32

4-
int add(int a,int b)
5-
{
6-
return a+b;
7-
}
3+
int add(int a, int b) { return a + b; }
84
}

src/aggregate_initialization.cpp

Lines changed: 43 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,58 @@
22

33
class entity {
44
public:
5-
int x;
6-
std::string y;
5+
int x;
6+
std::string y;
77
};
88

9-
class intArray
10-
{
9+
class intArray {
1110
public:
12-
int size;
13-
int * data;
11+
int size;
12+
int *data;
1413

15-
intArray(int size)
16-
{
17-
size = size;
18-
data = new int[size];
19-
}
20-
21-
~intArray()
22-
{
23-
delete[] data;
24-
}
14+
intArray(int size) {
15+
size = size;
16+
data = new int[size];
17+
}
2518

26-
intArray(std::initializer_list<int> list)
27-
{
28-
size = list.size();
29-
data = new int[list.size()];
19+
~intArray() { delete[] data; }
3020

31-
//std::initializer_list doesn't provide a subscript operator
21+
intArray(std::initializer_list<int> list) {
22+
size = list.size();
23+
data = new int[list.size()];
3224

33-
int count{ 0 };
34-
for (auto element : list)
35-
{
36-
data[count] = element;
37-
++count;
38-
}
25+
// std::initializer_list doesn't provide a subscript operator
3926

27+
int count{0};
28+
for (auto element : list) {
29+
data[count] = element;
30+
++count;
4031
}
32+
}
4133
};
4234

43-
int main()
44-
{
45-
// Aggregate initialization (Initializations using public attributes)
46-
entity entity1{ 2011,"name" };
47-
entity entity2 = { 2011,"name"};
48-
49-
50-
/*
51-
initializer_list
52-
so far the only way to use our array is like this:
53-
*/
54-
55-
intArray array1(4);
56-
array1.data[0] =7 ;
57-
array1.data[1] = 3;
58-
array1.data[2] = 5;
59-
array1.data[3] = 6;
60-
61-
/*
62-
with initializer_list, we can send an array directly:
63-
*/
64-
65-
66-
intArray array2({ 1,2,3,4,5,6,7,8 });
67-
std::cout << array2.data[0] << std::endl;
68-
std::cout << array2.data[1] << std::endl;
69-
std::cout << array2.data[2] << std::endl;
35+
int main() {
36+
// Aggregate initialization (Initializations using public attributes)
37+
entity entity1{2011, "name"};
38+
entity entity2 = {2011, "name"};
39+
40+
/*
41+
initializer_list
42+
so far the only way to use our array is like this:
43+
*/
44+
45+
intArray array1(4);
46+
array1.data[0] = 7;
47+
array1.data[1] = 3;
48+
array1.data[2] = 5;
49+
array1.data[3] = 6;
50+
51+
/*
52+
with initializer_list, we can send an array directly:
53+
*/
54+
55+
intArray array2({1, 2, 3, 4, 5, 6, 7, 8});
56+
std::cout << array2.data[0] << std::endl;
57+
std::cout << array2.data[1] << std::endl;
58+
std::cout << array2.data[2] << std::endl;
7059
}

0 commit comments

Comments
 (0)