Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar03 / src / dynamic-array / examples / variadic-templates.cpp
diff options
context:
space:
mode:
Commitdf74c873bc11ad90fb26e582c7be0ed092228661 (patch)
Author
 Stoycho Kyosev <skyosev@2a01-5a8-317-bbdc-4f2b-da5-db2e-fb02.v6.vivacom.bg>
2024-10-25 18:04:36 -0400
Committer
 Stoycho Kyosev <skyosev@2a01-5a8-317-bbdc-4f2b-da5-db2e-fb02.v6.vivacom.bg>
2024-10-25 18:04:36 -0400
Files73d2662eeddf5ebddfa6982270edd21fb4588de9 /Seminar03/src/dynamic-array/examples/variadic-templates.cpp
Data-structures-and-algorithms-df74c873bc11ad90fb26e582c7be0ed092228661.tar
Data-structures-and-algorithms-df74c873bc11ad90fb26e582c7be0ed092228661.tar.gz
Data-structures-and-algorithms-df74c873bc11ad90fb26e582c7be0ed092228661.zip
Parentc31c7c53f87104c254aac5ba99e984f9ec373d9a (diff)
Add sem03
1 files changed, 61 insertions, 0 deletions
-rw-r--r--Seminar03/src/dynamic-array/examples/variadic-templates.cpp61
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