Compare commits
5 commits
e1d1138653
...
eb41bede5e
Author | SHA1 | Date | |
---|---|---|---|
|
eb41bede5e | ||
|
c55321e6f1 | ||
|
b5bb0bc2ca | ||
|
022dffc9a4 | ||
|
0a18980aa3 |
13 changed files with 115 additions and 131 deletions
2
Makefile
2
Makefile
|
@ -18,7 +18,7 @@ clean:
|
|||
done
|
||||
|
||||
format:
|
||||
find . -type f \( -name "*.cpp" -o -name "*.h" -o -name "*.c" \) -exec clang-format -i {} \;
|
||||
find . -type f \( -name "*.cc" -o -name "*.cpp" -o -name "*.h" -o -name "*.c" \) -exec clang-format -i {} \;
|
||||
|
||||
|
||||
.PHONY: build clean test format
|
||||
|
|
30
junk/1.5.cc
30
junk/1.5.cc
|
@ -3,25 +3,25 @@
|
|||
using std::cout;
|
||||
|
||||
struct A {
|
||||
A() = default;
|
||||
A(int x) { val = x; }
|
||||
void print() { cout << "A(" << val << ")"; }
|
||||
int val;
|
||||
A() = default;
|
||||
A(int x) { val = x; }
|
||||
void print() { cout << "A(" << val << ")"; }
|
||||
int val;
|
||||
};
|
||||
|
||||
struct B {
|
||||
// B(int x) { a = A(x); }
|
||||
B(int x) : a(x) {}
|
||||
void print() {
|
||||
cout << "B(";
|
||||
a.print();
|
||||
cout << ")";
|
||||
}
|
||||
A a;
|
||||
// B(int x) { a = A(x); }
|
||||
B(int x) : a(x) {}
|
||||
void print() {
|
||||
cout << "B(";
|
||||
a.print();
|
||||
cout << ")";
|
||||
}
|
||||
A a;
|
||||
};
|
||||
|
||||
int main() {
|
||||
B b(10);
|
||||
b.print();
|
||||
cout << "\n";
|
||||
B b(10);
|
||||
b.print();
|
||||
cout << "\n";
|
||||
}
|
||||
|
|
82
junk/1.cc
82
junk/1.cc
|
@ -4,60 +4,60 @@ using std::cout;
|
|||
|
||||
// Rule of three:
|
||||
// If a class requires a user-defined destructor, a user-defined copy
|
||||
// constructor, or a user-defined copy assignment operator, it almost certainly
|
||||
// requires all three.
|
||||
// constructor, or a user-defined copy assignment operator, it almost
|
||||
// certainly requires all three.
|
||||
//
|
||||
|
||||
// Rule of five:
|
||||
// Because the presence of a user-defined (include = default or = delete
|
||||
// declared) destructor, copy-constructor, or copy-assignment operator prevents
|
||||
// implicit definition of the move constructor and the move assignment operator,
|
||||
// any class for which move semantics are desirable, has to declare all five
|
||||
// special member functions:
|
||||
// declared) destructor, copy-constructor, or copy-assignment operator
|
||||
// prevents implicit definition of the move constructor and the move
|
||||
// assignment operator, any class for which move semantics are desirable, has
|
||||
// to declare all five special member functions:
|
||||
|
||||
struct A {
|
||||
A() = default;
|
||||
/* Constructor */
|
||||
A(const int &x) { val = x; }
|
||||
/* Copy constructor */
|
||||
A(A &other) { other.val = this->val; }
|
||||
/* Move constructor */
|
||||
A(A &&other) { other.val = this->val; }
|
||||
/* Copy assignment operator */
|
||||
A &operator=(const A &other) { return *this; }
|
||||
~A() {}
|
||||
void print() { cout << "A(" << val << ")"; }
|
||||
int val;
|
||||
A() = default;
|
||||
/* Constructor */
|
||||
A(const int &x) { val = x; }
|
||||
/* Copy constructor */
|
||||
A(A &other) { other.val = this->val; }
|
||||
/* Move constructor */
|
||||
A(A &&other) { other.val = this->val; }
|
||||
/* Copy assignment operator */
|
||||
A &operator=(const A &other) { return *this; }
|
||||
~A() {}
|
||||
void print() { cout << "A(" << val << ")"; }
|
||||
int val;
|
||||
};
|
||||
|
||||
struct B {
|
||||
B(int x) { a = A(x); }
|
||||
// B(int x) : a(x) {};
|
||||
void print() {
|
||||
cout << "B(";
|
||||
a.print();
|
||||
cout << ")";
|
||||
}
|
||||
A a;
|
||||
B(int x) { a = A(x); }
|
||||
// B(int x) : a(x) {};
|
||||
void print() {
|
||||
cout << "B(";
|
||||
a.print();
|
||||
cout << ")";
|
||||
}
|
||||
A a;
|
||||
};
|
||||
|
||||
int main() {
|
||||
B b(10);
|
||||
b.print();
|
||||
cout << "\n";
|
||||
B b(10);
|
||||
b.print();
|
||||
cout << "\n";
|
||||
}
|
||||
|
||||
struct K {
|
||||
K() = default;
|
||||
/* Constructor */
|
||||
K(const int &x) { val = x; }
|
||||
/* Copy constructor */
|
||||
K(K &other) : K(other.val) {}
|
||||
/* Move constructor */
|
||||
K(K &&other) : K(other.val) {}
|
||||
/* Copy assignment operator */
|
||||
K &operator=(const K &other) { return *this; }
|
||||
~K() {}
|
||||
void print() { cout << "K(" << val << ")"; }
|
||||
int val;
|
||||
K() = default;
|
||||
/* Constructor */
|
||||
K(const int &x) { val = x; }
|
||||
/* Copy constructor */
|
||||
K(K &other) : K(other.val) {}
|
||||
/* Move constructor */
|
||||
K(K &&other) : K(other.val) {}
|
||||
/* Copy assignment operator */
|
||||
K &operator=(const K &other) { return *this; }
|
||||
~K() {}
|
||||
void print() { cout << "K(" << val << ")"; }
|
||||
int val;
|
||||
};
|
||||
|
|
14
junk/2.cc
14
junk/2.cc
|
@ -2,18 +2,16 @@
|
|||
#include <iostream>
|
||||
|
||||
void consume_number(int *p) {
|
||||
std::cout << "Consuming: " << *p << std::endl;
|
||||
delete p;
|
||||
std::cout << "Consuming: " << *p << std::endl;
|
||||
delete p;
|
||||
}
|
||||
|
||||
void random_int(int &out) { out = rand(); }
|
||||
|
||||
void use2() {
|
||||
int i;
|
||||
random_int(i);
|
||||
consume_number(&i);
|
||||
int i;
|
||||
random_int(i);
|
||||
consume_number(&i);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
use2();
|
||||
}
|
||||
int main(int argc, char *argv[]) { use2(); }
|
||||
|
|
48
junk/3.cc
48
junk/3.cc
|
@ -3,43 +3,43 @@
|
|||
|
||||
// In C we need a length in bytes to check for equality
|
||||
void compareObject(void *a, void *b, size_t len) {
|
||||
if (a == b)
|
||||
std::cout << "Same address" << std::endl;
|
||||
if (a == b)
|
||||
std::cout << "Same address" << std::endl;
|
||||
|
||||
// Being explicit about pointer type allows pointer arithmetic
|
||||
unsigned char *i1 = (unsigned char *)a;
|
||||
unsigned char *i2 = (unsigned char *)b;
|
||||
// Being explicit about pointer type allows pointer arithmetic
|
||||
unsigned char *i1 = (unsigned char *)a;
|
||||
unsigned char *i2 = (unsigned char *)b;
|
||||
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (i1[i] != i2[i]) {
|
||||
std::cout << "Different value" << std::endl;
|
||||
return;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (i1[i] != i2[i]) {
|
||||
std::cout << "Different value" << std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Same value" << std::endl;
|
||||
std::cout << "Same value" << std::endl;
|
||||
}
|
||||
|
||||
template <typename T> void cmp(T &a, T &b) {
|
||||
if (&a == &b)
|
||||
std::cout << "Same object" << std::endl;
|
||||
if (&a == &b)
|
||||
std::cout << "Same object" << std::endl;
|
||||
|
||||
if (a == b)
|
||||
std::cout << "Same value" << std::endl;
|
||||
else
|
||||
std::cout << "Different value" << std::endl;
|
||||
if (a == b)
|
||||
std::cout << "Same value" << std::endl;
|
||||
else
|
||||
std::cout << "Different value" << std::endl;
|
||||
}
|
||||
|
||||
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, c);
|
||||
cmp(a, b);
|
||||
cmp(a, c);
|
||||
|
||||
std::cout << "Checking C version" << std::endl;
|
||||
std::cout << "Checking C version" << std::endl;
|
||||
|
||||
compareObject(&a, &b, sizeof(int));
|
||||
compareObject(&a, &c, sizeof(int));
|
||||
compareObject(&a, &b, sizeof(int));
|
||||
compareObject(&a, &c, sizeof(int));
|
||||
}
|
||||
|
|
30
junk/4.cc
30
junk/4.cc
|
@ -1,21 +1,21 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <ranges>
|
||||
#include <vector>
|
||||
|
||||
template <typename InputIt, typename OutputIt, typename Pred>
|
||||
std::pair<InputIt, OutputIt> copy_while(InputIt first, InputIt last,
|
||||
OutputIt out, Pred p);
|
||||
|
||||
std::vector<int> take_while_sum_less_than(std::vector<int> &v, int n) {
|
||||
std::vector<int> out;
|
||||
int acc = 0;
|
||||
while (true) {
|
||||
if (v.back() + acc > n) {
|
||||
int next = v.back();
|
||||
out.push_back(n);
|
||||
v.pop_back();
|
||||
std::vector<int> out;
|
||||
int acc = 0;
|
||||
while (true) {
|
||||
if (v.back() + acc > n) {
|
||||
int next = v.back();
|
||||
out.push_back(n);
|
||||
v.pop_back();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#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;
|
||||
|
||||
auto result = v | std::ranges::views::take_while([&sum, n](int x) {
|
||||
if (sum + x < n) {
|
||||
sum += x;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
if (sum + x < n) {
|
||||
sum += x;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
return std::vector<int>(result.begin(), result.end());
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
#include <cstdint>
|
||||
template <typename T> class User {
|
||||
T id;
|
||||
T id;
|
||||
|
||||
public:
|
||||
explicit User(T n) : id(n) {}
|
||||
public:
|
||||
explicit User(T n) : id(n) {}
|
||||
};
|
||||
|
||||
int main() {
|
||||
User<int> user1{1};
|
||||
User<uint8_t> user2(0xFF);
|
||||
User<int> user1{1};
|
||||
User<uint8_t> user2(0xFF);
|
||||
}
|
||||
|
|
5
template/Makefile
Normal file
5
template/Makefile
Normal file
|
@ -0,0 +1,5 @@
|
|||
# Only define if needed:
|
||||
# TARGET = main.elf
|
||||
# SRCS = main.cc
|
||||
|
||||
include ../config.mk
|
3
template/main.cc
Normal file
3
template/main.cc
Normal file
|
@ -0,0 +1,3 @@
|
|||
#include <iostream>
|
||||
|
||||
int main() { std::cout << "Template" << std::endl; }
|
|
@ -1,6 +0,0 @@
|
|||
TARGET = main.elf
|
||||
SRCS = main.cc
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
include ../config.mk
|
|
@ -1,5 +0,0 @@
|
|||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
std::cout << "Hola" << std::endl;
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
TARGET = main.elf
|
||||
SRCS = main.cc
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
include ../config.mk
|
|
@ -1,5 +0,0 @@
|
|||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
std::cout << "Hola" << std::endl;
|
||||
}
|
Loading…
Reference in a new issue