Mass reformat

This commit is contained in:
Imbus 2025-01-11 17:54:29 +01:00
parent c55321e6f1
commit eb41bede5e
7 changed files with 107 additions and 111 deletions

View file

@ -3,25 +3,25 @@
using std::cout; using std::cout;
struct A { struct A {
A() = default; A() = default;
A(int x) { val = x; } A(int x) { val = x; }
void print() { cout << "A(" << val << ")"; } void print() { cout << "A(" << val << ")"; }
int val; int val;
}; };
struct B { struct B {
// B(int x) { a = A(x); } // B(int x) { a = A(x); }
B(int x) : a(x) {} B(int x) : a(x) {}
void print() { void print() {
cout << "B("; cout << "B(";
a.print(); a.print();
cout << ")"; cout << ")";
} }
A a; A a;
}; };
int main() { int main() {
B b(10); B b(10);
b.print(); b.print();
cout << "\n"; cout << "\n";
} }

View file

@ -4,60 +4,60 @@ using std::cout;
// Rule of three: // Rule of three:
// If a class requires a user-defined destructor, a user-defined copy // If a class requires a user-defined destructor, a user-defined copy
// constructor, or a user-defined copy assignment operator, it almost certainly // constructor, or a user-defined copy assignment operator, it almost
// requires all three. // certainly requires all three.
// //
// Rule of five: // Rule of five:
// Because the presence of a user-defined (include = default or = delete // Because the presence of a user-defined (include = default or = delete
// declared) destructor, copy-constructor, or copy-assignment operator prevents // declared) destructor, copy-constructor, or copy-assignment operator
// implicit definition of the move constructor and the move assignment operator, // prevents implicit definition of the move constructor and the move
// any class for which move semantics are desirable, has to declare all five // assignment operator, any class for which move semantics are desirable, has
// special member functions: // to declare all five special member functions:
struct A { struct A {
A() = default; A() = default;
/* Constructor */ /* Constructor */
A(const int &x) { val = x; } A(const int &x) { val = x; }
/* Copy constructor */ /* Copy constructor */
A(A &other) { other.val = this->val; } A(A &other) { other.val = this->val; }
/* Move constructor */ /* Move constructor */
A(A &&other) { other.val = this->val; } A(A &&other) { other.val = this->val; }
/* Copy assignment operator */ /* Copy assignment operator */
A &operator=(const A &other) { return *this; } A &operator=(const A &other) { return *this; }
~A() {} ~A() {}
void print() { cout << "A(" << val << ")"; } void print() { cout << "A(" << val << ")"; }
int val; int val;
}; };
struct B { struct B {
B(int x) { a = A(x); } B(int x) { a = A(x); }
// B(int x) : a(x) {}; // B(int x) : a(x) {};
void print() { void print() {
cout << "B("; cout << "B(";
a.print(); a.print();
cout << ")"; cout << ")";
} }
A a; A a;
}; };
int main() { int main() {
B b(10); B b(10);
b.print(); b.print();
cout << "\n"; cout << "\n";
} }
struct K { struct K {
K() = default; K() = default;
/* Constructor */ /* Constructor */
K(const int &x) { val = x; } K(const int &x) { val = x; }
/* Copy constructor */ /* Copy constructor */
K(K &other) : K(other.val) {} K(K &other) : K(other.val) {}
/* Move constructor */ /* Move constructor */
K(K &&other) : K(other.val) {} K(K &&other) : K(other.val) {}
/* Copy assignment operator */ /* Copy assignment operator */
K &operator=(const K &other) { return *this; } K &operator=(const K &other) { return *this; }
~K() {} ~K() {}
void print() { cout << "K(" << val << ")"; } void print() { cout << "K(" << val << ")"; }
int val; int val;
}; };

View file

@ -2,18 +2,16 @@
#include <iostream> #include <iostream>
void consume_number(int *p) { void consume_number(int *p) {
std::cout << "Consuming: " << *p << std::endl; std::cout << "Consuming: " << *p << std::endl;
delete p; delete p;
} }
void random_int(int &out) { out = rand(); } void random_int(int &out) { out = rand(); }
void use2() { void use2() {
int i; int i;
random_int(i); random_int(i);
consume_number(&i); consume_number(&i);
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) { use2(); }
use2();
}

View file

@ -3,43 +3,43 @@
// In C we need a length in bytes to check for equality // In C we need a length in bytes to check for equality
void compareObject(void *a, void *b, size_t len) { void compareObject(void *a, void *b, size_t len) {
if (a == b) if (a == b)
std::cout << "Same address" << std::endl; std::cout << "Same address" << std::endl;
// Being explicit about pointer type allows pointer arithmetic // Being explicit about pointer type allows pointer arithmetic
unsigned char *i1 = (unsigned char *)a; unsigned char *i1 = (unsigned char *)a;
unsigned char *i2 = (unsigned char *)b; unsigned char *i2 = (unsigned char *)b;
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
if (i1[i] != i2[i]) { if (i1[i] != i2[i]) {
std::cout << "Different value" << std::endl; std::cout << "Different value" << std::endl;
return; return;
}
} }
}
std::cout << "Same value" << std::endl; std::cout << "Same value" << std::endl;
} }
template <typename T> void cmp(T &a, T &b) { template <typename T> void cmp(T &a, T &b) {
if (&a == &b) if (&a == &b)
std::cout << "Same object" << std::endl; std::cout << "Same object" << std::endl;
if (a == b) if (a == b)
std::cout << "Same value" << std::endl; std::cout << "Same value" << std::endl;
else else
std::cout << "Different value" << std::endl; std::cout << "Different value" << std::endl;
} }
int main() { int main() {
int a = 10, b = 10, c = 12; int a = 10, b = 10, c = 12;
std::cout << "Checking template version" << std::endl; std::cout << "Checking template version" << std::endl;
cmp(a, b); cmp(a, b);
cmp(a, c); cmp(a, c);
std::cout << "Checking C version" << std::endl; std::cout << "Checking C version" << std::endl;
compareObject(&a, &b, sizeof(int)); compareObject(&a, &b, sizeof(int));
compareObject(&a, &c, sizeof(int)); compareObject(&a, &c, sizeof(int));
} }

View file

@ -1,21 +1,21 @@
#include <iostream> #include <iostream>
#include <vector>
#include <ranges> #include <ranges>
#include <vector>
template <typename InputIt, typename OutputIt, typename Pred> template <typename InputIt, typename OutputIt, typename Pred>
std::pair<InputIt, OutputIt> copy_while(InputIt first, InputIt last, std::pair<InputIt, OutputIt> copy_while(InputIt first, InputIt last,
OutputIt out, Pred p); OutputIt out, Pred p);
std::vector<int> take_while_sum_less_than(std::vector<int> &v, int n) { std::vector<int> take_while_sum_less_than(std::vector<int> &v, int n) {
std::vector<int> out; std::vector<int> out;
int acc = 0; int acc = 0;
while (true) { while (true) {
if (v.back() + acc > n) { if (v.back() + acc > n) {
int next = v.back(); int next = v.back();
out.push_back(n); out.push_back(n);
v.pop_back(); v.pop_back();
}
} }
}
} }
#if __cplusplus > 202002L #if __cplusplus > 202002L
@ -26,12 +26,12 @@ std::vector<int> take_while_sum_less_than(const std::vector<int> &v, int n) {
int sum = 0; int sum = 0;
auto result = v | std::ranges::views::take_while([&sum, n](int x) { auto result = v | std::ranges::views::take_while([&sum, n](int x) {
if (sum + x < n) { if (sum + x < n) {
sum += x; sum += x;
return true; return true;
} }
return false; return false;
}); });
return std::vector<int>(result.begin(), result.end()); return std::vector<int>(result.begin(), result.end());
} }

View file

@ -1,12 +1,12 @@
#include <cstdint> #include <cstdint>
template <typename T> class User { template <typename T> class User {
T id; T id;
public: public:
explicit User(T n) : id(n) {} explicit User(T n) : id(n) {}
}; };
int main() { int main() {
User<int> user1{1}; User<int> user1{1};
User<uint8_t> user2(0xFF); User<uint8_t> user2(0xFF);
} }

View file

@ -1,5 +1,3 @@
#include <iostream> #include <iostream>
int main() { int main() { std::cout << "Template" << std::endl; }
std::cout << "Template" << std::endl;
}