Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add strict_invoke #13

Open
lefticus opened this issue Aug 1, 2024 · 0 comments
Open

Add strict_invoke #13

lefticus opened this issue Aug 1, 2024 · 0 comments

Comments

@lefticus
Copy link
Owner

lefticus commented Aug 1, 2024

https://compiler-explorer.com/z/W33rTKzKG

#include <type_traits>
#include <functional>

template <typename Ret, typename... Arg, typename... Param>
constexpr auto strict_invoke(Ret (*f)(Arg...),
                             Param&&... param) noexcept(false) -> Ret
  requires(
      std::is_same_v<std::remove_cvref_t<Arg>, std::remove_cvref_t<Param>> &&
      ...)
      && std::is_invocable_v<decltype(f), decltype(param)...>

{
  return f(std::forward<Param>(param)...);
}

double add(double lhs, double rhs) {
  return lhs + rhs;
}

int main()
{
  add(2.3f, 1.3f); // works
  add(2, 1); // works
  //strict_invoke(&add, 2.3f, 1.3f); // fails, requires conversion
  strict_invoke(&add, 1.3, 1.3); // works
  //strict_invoke(&add, 1, 2); // fails, requires conversion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant