diff options
Add sem03
2 files changed, 88 insertions, 0 deletions
| -rw-r--r-- | Seminar03/src/dynamic-array/examples/placement-new-memory-strategy.cpp | 27 | ||||
| -rw-r--r-- | Seminar03/src/dynamic-array/examples/variadic-templates.cpp | 61 |
diff --git a/Seminar03/src/dynamic-array/examples/placement-new-memory-strategy.cpp b/Seminar03/src/dynamic-array/examples/placement-new-memory-strategy.cpp new file mode 100644 index 0000000..693b95c --- /dev/null +++ b/Seminar03/src/dynamic-array/examples/placement-new-memory-strategy.cpp @@ -0,0 +1,27 @@ +#include <iostream> + +struct E +{ + E() + { + std::cout << "constr" << std::endl; + } + + ~E() + { + std::cout << "destr" << std::endl; + } +}; + +int main() +{ + std::cout << "operator new called" << std::endl; + E* e = static_cast<E*>(operator new(sizeof(E))); + std::cout << "placement new called" << std::endl; + new (e) E(); + + std::cout << "destructor manually called" << std::endl; + e->~E(); + std::cout << "operator delete called" << std::endl; + operator delete(e, sizeof(E)); +}
\ No newline at end of file diff --git a/Seminar03/src/dynamic-array/examples/variadic-templates.cpp b/Seminar03/src/dynamic-array/examples/variadic-templates.cpp new file mode 100644 index 0000000..291d81a --- /dev/null +++ b/Seminar03/src/dynamic-array/examples/variadic-templates.cpp @@ -0,0 +1,61 @@ +#include <iostream> + +// std::forward example - preserve the value category (lvalue or rvalue) +void f(const int& x) +{ + std::cout << "lvalue" << std::endl; +} + +void f(const int&& x) +{ + std::cout << "rvalue" << std::endl; +} + +template<class T> +void g(T&& x) +{ + f(std::forward<T>(x)); +} + +struct printer +{ + static void print() + { + std::cout << std::endl; + } + + template<class T, class... Args> + static void print(const T& first, const Args&... args) + { + std::cout << first << " "; + print(std::forward<const Args&>(args)...); + } +}; + +struct Entity +{ + int x; + friend std::ostream& operator<<(std::ostream& os, const Entity& e); + + Entity(int x_) : x(x_) {} + + Entity(Entity&) + { + std::cout << "Copied " << x << std::endl; + } +}; + +std::ostream& operator<<(std::ostream& os, const Entity& e) +{ + os << e.x; + return os; +} + +int main() +{ + int x = 10; + g(x); + + g(std::move(x)); + +}
\ No newline at end of file |
