From f838e7c4aaa04d5084f6922aa516a845b19e78e9 Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Fri, 4 Oct 2024 20:10:05 +0200 Subject: fn: add simple fn signature checker example --- fn_alias.cc | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 fn_alias.cc (limited to 'fn_alias.cc') 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 +#include + +// 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 +auto add(T t, U u) -> decltype(t + u) { + return t + u; +} + +template +inline constexpr auto add_int = add; + +// Test me. + +int main() { + say(); + say_alias(); + + assert(add(2, 3) == add_int(3, 2)); +} -- cgit v1.2.3