diff --git a/14.cpp b/14.cpp new file mode 100644 index 0000000..5781b32 --- /dev/null +++ b/14.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +template +concept EnoughMemory = (sizeof(Types) + ... + 0) <= SIZE; + +template +concept AllCopyConstructable = (std::is_copy_constructible_v && ...); + +template +requires EnoughMemory && AllCopyConstructable +void allocate(void* memory, Types... args) { + auto ptr = static_cast(memory); + auto copy = [&ptr](const T& arg) { + new (ptr) T(arg); + ptr += sizeof(T); + }; + (copy(args), ...); +} +/* +int main() { + constexpr size_t SIZE = 17; + char static_arr[SIZE]; + int a; + std::cin >> a; + allocate(static_arr, a, 4, 'a', "hi"); + + int val1 = *reinterpret_cast(static_arr); + assert(val1 == a); + int val2 = *reinterpret_cast(static_arr + sizeof(int)); + assert(val2 == 4); + char val3 = static_arr[sizeof(int) * 2]; + assert(val3 == 'a'); + const char* val4 = *reinterpret_cast(static_arr + sizeof(int) * 2 + sizeof(char)); + assert(std::strcmp(val4, "hi") == 0); +} +*/ \ No newline at end of file diff --git a/15.cpp b/15.cpp new file mode 100644 index 0000000..ce7ec26 --- /dev/null +++ b/15.cpp @@ -0,0 +1,49 @@ +#include + +#include "14.cpp" +#include "2.cpp" + +template +class Container { + size_t size{}; + char* data{}; + std::vector types; + +public: + Container(Types... args): size((sizeof(Types) + ... + 0)), data(new char[size]), types{sizeof(Types)...} { + allocate<(sizeof(Types) + ... + 0)>(data, args...); + } + ~Container() { delete[] data; } + + Container(const Container& other) = delete; + Container& operator=(const Container& other) = delete; + + Container(Container&& other) { + std::swap(size, other.size); + std::swap(data, other.data); + std::swap(types, other.types); + } + Container& operator=(Container&& other) { + std::swap(size, other.size); + std::swap(data, other.data); + std::swap(types, other.types); + return *this; + } + + template + T getElement(const size_t idx) { + assert(sizeof(T) == types[idx]); + size_t offset = 0; + for (size_t i = 0; i < idx; ++i) offset += types[i]; + return *reinterpret_cast(data + offset); + } +}; + +int main() { + Container c(12, 'c', Point{3, 4}); + Container c2; + Container c3((std::move(c))); + std::cout << c3.getElement(0) << std::endl; + std::cout << c3.getElement(1) << std::endl; + std::cout << c3.getElement(2) << std::endl; +} diff --git a/2.cpp b/2.cpp index a20a804..f425e12 100644 --- a/2.cpp +++ b/2.cpp @@ -12,6 +12,11 @@ struct Point { friend bool operator==(const Point& p1, const Point& p2) { return std::abs(p1.x - p2.x) < eps && std::abs(p1.y - p2.y) < eps; } + + friend std::ostream& operator<<(std::ostream& os, const Point& point) { + os << "(" << point.x << ", " << point.y << ")"; + return os; + } }; class Line { @@ -58,7 +63,7 @@ class Line { } }; - +/* int main() { Point p1{42, 17}; Point p2{42, 17}; auto invalid_line = Line::fabric(p1, p2); @@ -78,4 +83,5 @@ int main() { Line perp = line.perpendicular({1, 1}); auto inter_perp = line.intersection(perp); assert(inter_perp.has_value()); -} \ No newline at end of file +} +*/ \ No newline at end of file