镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Add multiple dispatch tool #14

Open

Description

@lefticus
#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
}

https://compiler-explorer.com/z/8sxMd1aj5

Activity

  1. changed the title [-]Add multiple dispath tool[/-] [+]Add multiple dispatch tool[/+] on Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions