summaryrefslogtreecommitdiff
path: root/fn_alias.cc
diff options
context:
space:
mode:
authorJohannes Stoelp <johannes.stoelp@gmail.com>2024-10-04 20:10:05 +0200
committerJohannes Stoelp <johannes.stoelp@gmail.com>2024-10-04 20:17:06 +0200
commitf838e7c4aaa04d5084f6922aa516a845b19e78e9 (patch)
treebbc68e7d9c059fc9593b55500ec59d37c593da5a /fn_alias.cc
parent0bb7c1ccf2fef23cbe0e263614fc4050b96305ea (diff)
downloadcpp-templates-f838e7c4aaa04d5084f6922aa516a845b19e78e9.tar.gz
cpp-templates-f838e7c4aaa04d5084f6922aa516a845b19e78e9.zip
fn: add simple fn signature checker example
Diffstat (limited to 'fn_alias.cc')
-rw-r--r--fn_alias.cc32
1 files changed, 32 insertions, 0 deletions
diff --git a/fn_alias.cc b/fn_alias.cc
new file mode 100644
index 0000000..2d9fd1d
--- /dev/null
+++ b/fn_alias.cc
@@ -0,0 +1,32 @@
+#include <cassert>
+#include <cstdio>
+
+// Function alias.
+
+void say() {
+ std::puts("say");
+}
+
+inline constexpr auto say_alias = say;
+
+// Template function alias.
+//
+// One drawback is that template type deducation does not work through the
+// function pointer. One would need to write some macro wrapper.
+
+template <typename T, typename U>
+auto add(T t, U u) -> decltype(t + u) {
+ return t + u;
+}
+
+template <typename U>
+inline constexpr auto add_int = add<int, U>;
+
+// Test me.
+
+int main() {
+ say();
+ say_alias();
+
+ assert(add(2, 3) == add_int<int>(3, 2));
+}