aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/development/c++
diff options
context:
space:
mode:
Diffstat (limited to 'src/development/c++')
-rw-r--r--src/development/c++/Makefile2
-rw-r--r--src/development/c++/fwd.cc58
2 files changed, 59 insertions, 1 deletions
diff --git a/src/development/c++/Makefile b/src/development/c++/Makefile
index ca752d9..36065e4 100644
--- a/src/development/c++/Makefile
+++ b/src/development/c++/Makefile
@@ -1,4 +1,4 @@
-SRC = concepts-11.cc meta.cc meta2.cc meta4.cc tmpl-pair.cc tmpl-void_t.cc
+SRC = concepts-11.cc meta.cc meta2.cc meta4.cc tmpl-pair.cc tmpl-void_t.cc fwd.cc
BIN = $(SRC:.cc=)
all: $(BIN)
diff --git a/src/development/c++/fwd.cc b/src/development/c++/fwd.cc
new file mode 100644
index 0000000..802d5bb
--- /dev/null
+++ b/src/development/c++/fwd.cc
@@ -0,0 +1,58 @@
+// Copyright (C) 2023 johannst
+
+#include <cstdio>
+#include <utility>
+
+struct M {};
+
+// -- CONSUMER -----------------------------------------------------------------
+
+void use(M&) {
+ puts(__PRETTY_FUNCTION__);
+}
+
+void use(M&&) {
+ puts(__PRETTY_FUNCTION__);
+}
+
+// -- TESTER -------------------------------------------------------------------
+
+template<typename T>
+void wrapper(T&& param) { // forwarding reference
+ puts(__PRETTY_FUNCTION__);
+ // PARAM is an lvalue, therefore this always calls use(M&).
+ use(param);
+}
+
+template<typename T>
+void fwd_wrapper(T&& param) { // forwarding reference
+ puts(__PRETTY_FUNCTION__);
+ // PARAM is an lvalue, but std::forward returns PARAM with the same value
+ // category as the forwarding reference takes.
+ use(std::forward<T>(param));
+}
+
+// -- MAIN ---------------------------------------------------------------------
+
+int main() {
+ {
+ std::puts("==> wrapper rvalue reference");
+ wrapper(M{});
+ // calls use(M&).
+
+ std::puts("==> wrapper lvalue reference");
+ struct M m;
+ wrapper(m);
+ // calls use(M&).
+ }
+ {
+ std::puts("==> fwd_wrapper rvalue reference");
+ fwd_wrapper(M{});
+ // calls use(M&&).
+
+ std::puts("==> fwd_wrapper lvalue reference");
+ struct M m;
+ fwd_wrapper(m);
+ // calls use(M&).
+ }
+}