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

Added noexcept variants to function traits #297

Closed
wants to merge 0 commits into from

Conversation

LuSo58
Copy link
Contributor

@LuSo58 LuSo58 commented Apr 4, 2024

When using function_traits with callable items that are noexcept, no specialization would match. Here is a simple reproduction case for this.

#include <iostream>
#include <functional>

#include "fplus/fplus.hpp"

template<typename F, typename T = typename fplus::utils::function_traits<F>::template arg<0>::type>
auto forward(F f) {
  return [f](auto x) {
    auto ret = f(x);
    return ret;
  };
}

class klass {
  public:
    int x;

    int normal() const {
      return x;
    }

    int noexc() const noexcept {
      return x;
    }
};

int half(int x) {
  return x / 2;
}

int half_noexc(int x) noexcept {
  return x / 2;
}

struct functor {
  int operator()(int x) {
    return x + 5;
  }
};

struct functor_noexc {
  int operator()(int x) noexcept {
    return x + 5;
  }
};

int main() {
  klass k = {42};
  std::cout << forward(std::mem_fn(&klass::normal))(k) << std::endl; // OK
  std::cout << forward(std::mem_fn(&klass::noexc))(k) << std::endl; // Will not compile
  std::cout << forward(half)(42) << std::endl; // OK
  std::cout << forward(half_noexc)(42) << std::endl; // Will not compile
  std::cout << forward(functor())(42) << std::endl; // OK
  std::cout << forward(functor_noexc())(42) << std::endl; // Will not compile
  auto lambda = [](int x) {
    return x + 10;
  };
  auto lambda_noexc = [](int x) noexcept {
    return x + 10;
  };
  std::cout << forward(lambda)(42) << std::endl; // OK
  std::cout << forward(lambda_noexc)(42) << std::endl; // Will not compile
}

@LuSo58 LuSo58 marked this pull request as ready for review April 4, 2024 15:48
@Dobiasd
Copy link
Owner

Dobiasd commented Apr 4, 2024

Oh, very good finding, thanks a lot! 😍

(And since the source repo of the function traits seems to no longer be maintained, I guess it does not make sense to try to include it there.)

@LuSo58
Copy link
Contributor Author

LuSo58 commented Apr 4, 2024

I added macros to hide the noexcept specs from compilers, that don't seem to distinguish between the signatures. See the checks results. I don't know how to force rerun the checks, so I can't check if it works easily.

@Dobiasd
Copy link
Owner

Dobiasd commented Apr 4, 2024

Thanks!

Yeah, I need to approve such runs - probably to prevent people from using the CI to mine Bitcoins or similar. ;)


Edit: Just for later reference. It continues here: #298

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

Successfully merging this pull request may close these issues.

2 participants