From 4adde6d349915be06e348302c4b0e075d53e1dca Mon Sep 17 00:00:00 2001 From: Aaron Date: Tue, 1 Aug 2023 20:05:42 +0000 Subject: [PATCH] [SymForce] Fix function_traits for `(*const&)` function pointers Topic: sf-constref-fp GitOrigin-RevId: a1ede3c8b4f411652f86c8f896384bb98df9e4e1 --- symforce/opt/templates.h | 41 +++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/symforce/opt/templates.h b/symforce/opt/templates.h index 5c92dcea5..bf4e1af7f 100644 --- a/symforce/opt/templates.h +++ b/symforce/opt/templates.h @@ -10,12 +10,7 @@ namespace sym { // ------------------------------------------------------------------------------------------------ -// Function traits -// -// Extracts the number of arguments and types of the arguments and return value. -// Handle generic functors by looking at the 'operator()'. -// ------------------------------------------------------------------------------------------------ - +// C++14 implementation of remove_cvref template struct remove_cvref { using type = std::remove_cv_t>; @@ -24,8 +19,19 @@ struct remove_cvref { template using remove_cvref_t = typename remove_cvref::type; +// ------------------------------------------------------------------------------------------------ +// Function traits +// +// Extracts the number of arguments and types of the arguments and return value. +// Handles: +// - Function pointers +// - Member function pointers +// - Functors (objects with operator()) +// - Lambdas +// ------------------------------------------------------------------------------------------------ + template -struct function_traits : public function_traits::operator())> {}; +struct function_traits; // Traits implementation template @@ -44,12 +50,20 @@ struct function_traits { }; }; -// Specialize for function pointers template -struct function_traits : public function_traits {}; +constexpr std::size_t function_traits::num_arguments; -template -struct function_traits : public function_traits {}; +// Specializations to remove type modifiers +template +struct function_traits : public function_traits {}; +template +struct function_traits : public function_traits {}; +template +struct function_traits : public function_traits {}; +template +struct function_traits : public function_traits {}; +template +struct function_traits : public function_traits {}; // Specialize for member function pointers template @@ -61,4 +75,9 @@ template struct function_traits : public function_traits {}; +// ------------------------------------------------------------------------------------------------ +// Specialize for functors +template +struct function_traits : public function_traits {}; + } // namespace sym