Skip to content

Commit f6ea82c

Browse files
committed
Add .clang-format and format the code
1 parent da0dfbf commit f6ea82c

20 files changed

+253
-251
lines changed

.clang-format

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Language: Cpp
2+
BasedOnStyle: Google

src/auto_vs_decltype_main.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
// Helper objects/functions
3030
//========================================================================
3131
template <typename T, typename S>
32-
auto multiply(T lhs, S rhs)->decltype(lhs * rhs) {
32+
auto multiply(T lhs, S rhs) -> decltype(lhs * rhs) {
3333
return lhs * rhs;
3434
}
3535

36-
template<typename T, typename S>
36+
template <typename T, typename S>
3737
auto fpmin(T x, S y) -> decltype(x < y ? x : y) {
3838
return x < y ? x : y;
3939
}
@@ -58,10 +58,10 @@ int main() {
5858
}
5959

6060
// 2. auto INSTEAD OF TYPE SPECIFIER FOR SCALAR VARS
61-
int x = int(); // x is an int, initialized to 0
61+
int x = int(); // x is an int, initialized to 0
6262
assert(x == 0);
6363

64-
const int &crx = x; // crx is a const int& that refers to x
64+
const int &crx = x; // crx is a const int& that refers to x
6565
x = 42;
6666
assert(crx == 42 && x == 42);
6767

@@ -84,7 +84,7 @@ int main() {
8484
#ifdef COMPILATION_ERROR
8585
const int c = 0;
8686
auto &rc = c;
87-
rc = 44; // error: const qualifier was not removed
87+
rc = 44; // error: const qualifier was not removed
8888
#endif
8989

9090
// 3. decltype (multiply uses decltype)

src/const_vs_constexpr_main.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ constexpr int gcd_cpp11(int a, int b) {
3030

3131
// C++14 implementation - many restrictions are relaxed, e.g. loops and auto
3232
// are now allowed
33-
#if __cplusplus >= 201403L
33+
#if __cplusplus >= 201403L
3434
constexpr auto gcd_cpp14(int a, int b) {
3535
while (b != 0) {
3636
auto t = b;
@@ -45,14 +45,21 @@ constexpr auto gcd_cpp14(int a, int b) {
4545
// The following functions implement the Fibonacci sequence calculation. The
4646
// 1st implementation uses templates, and the 2nd constexpr.
4747
//------------------------------------------------------------------------
48-
template <unsigned n> struct fibonacci_tmp {
48+
template <unsigned n>
49+
struct fibonacci_tmp {
4950
static const unsigned value =
5051
fibonacci_tmp<n - 1>::value + fibonacci_tmp<n - 2>::value;
5152
};
5253

53-
template <> struct fibonacci_tmp<0> { static const unsigned value = 0; };
54+
template <>
55+
struct fibonacci_tmp<0> {
56+
static const unsigned value = 0;
57+
};
5458

55-
template <> struct fibonacci_tmp<1> { static const unsigned value = 1; };
59+
template <>
60+
struct fibonacci_tmp<1> {
61+
static const unsigned value = 1;
62+
};
5663

5764
constexpr unsigned fibonacci_cxp(const unsigned x) {
5865
return x <= 1 ? x : fibonacci_cxp(x - 1) + fibonacci_cxp(x - 2);
@@ -65,19 +72,23 @@ int main() {
6572
// 1. CONST VS COMPILE-TIME-CONST
6673
// Integers with const qualifiers are compile time constants. Doubles aren't
6774
// and require constexpr to become compile time consts.
68-
const int var_a = 10; // can be evaluated at compile time (i.e. compile time constant)
75+
const int var_a =
76+
10; // can be evaluated at compile time (i.e. compile time constant)
6977
#ifdef COMPILATION_ERROR
70-
const double var_b = 3.8; // cannot be evaluated at compile time (i.e. run-time constant)
78+
const double var_b =
79+
3.8; // cannot be evaluated at compile time (i.e. run-time constant)
7180
#else
72-
constexpr double var_b = 3.8; // can be evaluated at compile time (i.e. compile time constant)
81+
constexpr double var_b =
82+
3.8; // can be evaluated at compile time (i.e. compile time constant)
7383
#endif
7484

75-
const int var_c = var_a + 5; // compile time constant
76-
constexpr double var_d = var_b - 2.7; // compile time constant
85+
const int var_c = var_a + 5; // compile time constant
86+
constexpr double var_d = var_b - 2.7; // compile time constant
7787

7888
// 2. CONST vs CONSTEXPR for pointers
7989
const int *ptr_e;
80-
constexpr int *ptr_f = nullptr; // equivalent to int *const ptr_f (as opposed to const int *ptr_f)
90+
constexpr int *ptr_f = nullptr; // equivalent to int *const ptr_f (as opposed
91+
// to const int *ptr_f)
8192

8293
// 3. GCD
8394
// Compile time calculations

src/deep_vs_shallow.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010
//
1111
// License: MIT
1212
//========================================================================
13-
#include <iostream>
1413
#include <deep_vs_shallow.hpp>
14+
#include <iostream>
1515

1616
Shallow::Shallow() { m_data = new int[m_capacity]; }
1717

18-
Shallow::Shallow(const Shallow &rhs) {
19-
m_data = rhs.m_data;
20-
}
18+
Shallow::Shallow(const Shallow& rhs) { m_data = rhs.m_data; }
2119

2220
Shallow::~Shallow() {
2321
if (m_data != nullptr) {
@@ -26,8 +24,7 @@ Shallow::~Shallow() {
2624
}
2725
}
2826

29-
void
30-
Shallow::add(int elem) {
27+
void Shallow::add(int elem) {
3128
if (m_num_elements >= m_capacity) {
3229
std::cout << "Full buffer, exiting." << std::endl;
3330
return;
@@ -37,14 +34,11 @@ Shallow::add(int elem) {
3734
m_num_elements++;
3835
}
3936

40-
int&
41-
Shallow::getElemAt(unsigned idx) { return m_data[idx]; }
37+
int& Shallow::getElemAt(unsigned idx) { return m_data[idx]; }
4238

43-
Deep::Deep() {
44-
m_data = new int[m_capacity]();
45-
}
39+
Deep::Deep() { m_data = new int[m_capacity](); }
4640

47-
Deep::Deep(const Deep &rhs) {
41+
Deep::Deep(const Deep& rhs) {
4842
m_data = new int[m_capacity];
4943
for (size_t ii = 0; ii < m_capacity; ii++) {
5044
m_data[ii] = rhs.m_data[ii];
@@ -53,8 +47,7 @@ Deep::Deep(const Deep &rhs) {
5347

5448
Deep::~Deep() { delete[] m_data; }
5549

56-
void
57-
Deep::add(int elem) {
50+
void Deep::add(int elem) {
5851
if (m_num_elements >= m_capacity) {
5952
std::cout << "Full buffer, exiting." << std::endl;
6053
return;
@@ -64,5 +57,4 @@ Deep::add(int elem) {
6457
m_num_elements++;
6558
}
6659

67-
int&
68-
Deep::getElemAt(unsigned idx) { return m_data[idx]; }
60+
int& Deep::getElemAt(unsigned idx) { return m_data[idx]; }

src/deep_vs_shallow_main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <deep_vs_shallow.hpp>
1818
#include "cpp_tutor.h"
1919

20-
2120
int main() {
2221
// 1. SHALLOW COPY
2322
Shallow obj_a;

src/memory_block.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ MemoryBlock::MemoryBlock(const MemoryBlock &other)
3838
std::copy(other.m_data, other.m_data + m_length, m_data);
3939
}
4040

41-
MemoryBlock&
42-
MemoryBlock::operator=(const MemoryBlock &other) {
41+
MemoryBlock &MemoryBlock::operator=(const MemoryBlock &other) {
4342
std::cout << "In operator=(const MemoryBlock&). length = " << other.m_length
4443
<< ". Copying resource." << std::endl;
4544

@@ -54,10 +53,7 @@ MemoryBlock::operator=(const MemoryBlock &other) {
5453
return *this;
5554
}
5655

57-
size_t
58-
MemoryBlock::length() const {
59-
return m_length;
60-
}
56+
size_t MemoryBlock::length() const { return m_length; }
6157

6258
MemoryBlock::MemoryBlock(MemoryBlock &&other) noexcept
6359
: m_length(0), m_data(nullptr) {
@@ -75,8 +71,7 @@ MemoryBlock::MemoryBlock(MemoryBlock &&other) noexcept
7571
other.m_length = 0;
7672
}
7773

78-
MemoryBlock&
79-
MemoryBlock::operator=(MemoryBlock &&other) noexcept {
74+
MemoryBlock &MemoryBlock::operator=(MemoryBlock &&other) noexcept {
8075
std::cout << "In operator=(MemoryBlock&&). length = " << other.m_length << "."
8176
<< std::endl;
8277

src/noexcept_main.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,22 @@
99
// Illustrates the noexcept keyword and demonstrates that std::vector has
1010
// different behavior if the type's move constructor is marked noexcept. This
1111
// reason behind this is very well explained here:
12-
// * https://www.reddit.com/r/cpp_questions/comments/51btqw/why_does_stdvector_have_different_behavior_if_the/
12+
// *
13+
// https://www.reddit.com/r/cpp_questions/comments/51btqw/why_does_stdvector_have_different_behavior_if_the/
1314
//
1415
// License: MIT
1516
//========================================================================
16-
#include <vector>
1717
#include <iostream>
18+
#include <vector>
1819

1920
//========================================================================
2021
// Helper functions and data structures
2122
//========================================================================
2223
// Throwing in foo triggers a call to std::terminate
23-
void foo() noexcept(true) {
24-
throw 13;
25-
}
24+
void foo() noexcept(true) { throw 13; }
2625

2726
// Throwing in bar is fine
28-
void bar() noexcept(false) {
29-
throw 13;
30-
}
27+
void bar() noexcept(false) { throw 13; }
3128

3229
// A is a trivial class with it's move constructor marked as noexcept. Note
3330
// that move-constructing means that the id of the object moved-from is
@@ -54,7 +51,7 @@ struct A {
5451
}
5552

5653
// Move assignment operator.
57-
A &operator=(A &&other) = delete;
54+
A &operator=(A &&other) = delete;
5855

5956
// Destructor.
6057
~A() { std::cout << " A: destructing object: " << id << std::endl; }
@@ -139,7 +136,7 @@ int main() {
139136
// 3. THROW AN EXCEPTION INSIDE A FUNCTION DECORATED WITH noexcept(false)
140137
try {
141138
bar();
142-
} catch(...) {
139+
} catch (...) {
143140
std::cout << "Exception in bar caught" << std::endl;
144141
}
145142

@@ -152,7 +149,7 @@ int main() {
152149

153150
try {
154151
foo();
155-
} catch(...) {
152+
} catch (...) {
156153
std::cout << "Exception in foo caught" << std::endl;
157154
}
158155
}

src/null_vs_nullptr_main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
//
1616
// License: MIT
1717
//========================================================================
18-
#include "cpp_tutor.h"
1918
#include <cassert>
2019
#include <iostream>
20+
#include "cpp_tutor.h"
2121

2222
//========================================================================
2323
// Helper functions

src/override_final_main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ struct B final : A
3636
};
3737

3838
struct C : B {
39-
void foo(){}
39+
void foo() {}
4040
#ifdef COMPILATION_ERROR
41-
void fez(){}
41+
void fez() {}
4242
#endif
4343
};
4444

0 commit comments

Comments
 (0)