Skip to content

Could not construct fu2::unique_function from a pointer to member function on MSVC #71

Open

Description

@Serikov

Compiling this example in MSVC v19.43 VS17.13 godbolt link gives an error:
more than one partial specialization matches the template argument list

Example code:

#include "function2/function2.hpp"
#include <functional>

struct Foo {
  std::size_t bar() const {
    return 5;
  }
};

int main() {
    std::function<std::size_t(const Foo&)> f(&Foo::bar); // compiles
    fu2::unique_function<std::size_t(const Foo&)> f2(&Foo::bar); // gives an error
}

Activity

  1. Kim-J-Smith commented on Feb 10, 2026

    @Kim-J-Smith
    diff --git a/include/function2/function2.hpp b/include/function2/function2.hpp
    index 6453f70..dfacbfd 100644
    --- a/include/function2/function2.hpp
    +++ b/include/function2/function2.hpp
    @@ -213,45 +213,13 @@ struct property {
     
     /// Provides utilities for invocing callable objects
     namespace invocation {
    -/// Invokes the given callable object with the given arguments
    -template <typename Callable, typename... Args>
    -constexpr auto invoke(Callable&& callable, Args&&... args) noexcept(
    -    noexcept(std::forward<Callable>(callable)(std::forward<Args>(args)...)))
    -    -> decltype(std::forward<Callable>(callable)(std::forward<Args>(args)...)) {
     
    -  return std::forward<Callable>(callable)(std::forward<Args>(args)...);
    -}
    -/// Invokes the given member function pointer by reference
    -template <typename T, typename Type, typename Self, typename... Args>
    -constexpr auto invoke(Type T::*member, Self&& self, Args&&... args) noexcept(
    -    noexcept((std::forward<Self>(self).*member)(std::forward<Args>(args)...)))
    -    -> decltype((std::forward<Self>(self).*
    -                 member)(std::forward<Args>(args)...)) {
    -  return (std::forward<Self>(self).*member)(std::forward<Args>(args)...);
    -}
    -/// Invokes the given member function pointer by pointer
    -template <typename T, typename Type, typename Self, typename... Args>
    -constexpr auto invoke(Type T::*member, Self&& self, Args&&... args) noexcept(
    -    noexcept((std::forward<Self>(self)->*member)(std::forward<Args>(args)...)))
    -    -> decltype((std::forward<Self>(self)->*member)(
    -        std::forward<Args>(args)...)) {
    -  return (std::forward<Self>(self)->*member)(std::forward<Args>(args)...);
    -}
    -/// Invokes the given pointer to a scalar member by reference
    -template <typename T, typename Type, typename Self>
    -constexpr auto
    -invoke(Type T::*member,
    -       Self&& self) noexcept(noexcept(std::forward<Self>(self).*member))
    -    -> decltype(std::forward<Self>(self).*member) {
    -  return (std::forward<Self>(self).*member);
    -}
    -/// Invokes the given pointer to a scalar member by pointer
    -template <typename T, typename Type, typename Self>
    -constexpr auto
    -invoke(Type T::*member,
    -       Self&& self) noexcept(noexcept(std::forward<Self>(self)->*member))
    -    -> decltype(std::forward<Self>(self)->*member) {
    -  return std::forward<Self>(self)->*member;
    +/// Use std::invoke and std::is_nothrow_invocable.
    +template <typename Callable, typename... Args>
    +constexpr auto invoke(Callable&& callable, Args&&... args)
    +noexcept(std::is_nothrow_invocable<Callable, Args...>::value) {
    +  return std::invoke(std::forward<Callable>(callable),
    +    std::forward<Args>(args)...);
     }
     
     /// Deduces to a true type if the callable object can be invoked with
    @@ -260,38 +228,9 @@ invoke(Type T::*member,
     /// SFINAE here.
     template <typename T, typename Args, typename = void>
     struct can_invoke : std::false_type {};
    -template <typename T, typename... Args>
    -struct can_invoke<T, identity<Args...>,
    -                  decltype((void)std::declval<T>()(std::declval<Args>()...))>
    -    : std::true_type {};
    -template <typename Pointer, typename T, typename... Args>
    -struct can_invoke<Pointer, identity<T&, Args...>,
    -                  decltype((void)((std::declval<T&>().*std::declval<Pointer>())(
    -                      std::declval<Args>()...)))> : std::true_type {};
    -template <typename Pointer, typename T, typename... Args>
    -struct can_invoke<Pointer, identity<T&&, Args...>,
    -                  decltype((
    -                      void)((std::declval<T&&>().*std::declval<Pointer>())(
    -                      std::declval<Args>()...)))> : std::true_type {};
    -template <typename Pointer, typename T, typename... Args>
    -struct can_invoke<Pointer, identity<T*, Args...>,
    -                  decltype((
    -                      void)((std::declval<T*>()->*std::declval<Pointer>())(
    -                      std::declval<Args>()...)))> : std::true_type {};
    -template <typename Pointer, typename T>
    -struct can_invoke<Pointer, identity<T&>,
    -                  decltype((void)(std::declval<T&>().*std::declval<Pointer>()))>
    -    : std::true_type {};
    -template <typename Pointer, typename T>
    -struct can_invoke<Pointer, identity<T&&>,
    -                  decltype((void)(std::declval<T&&>().*
    -                                  std::declval<Pointer>()))> : std::true_type {
    -};
    -template <typename Pointer, typename T>
    -struct can_invoke<Pointer, identity<T*>,
    -                  decltype((
    -                      void)(std::declval<T*>()->*std::declval<Pointer>()))>
    -    : std::true_type {};
    +template <typename Callee, typename... Args>
    +struct can_invoke<Callee, identity<Args...>, void>
    +: public std::is_invocable<Callee, Args...> {};
     
     template <bool RequiresNoexcept, typename T, typename Args>
     struct is_noexcept_correct : std::true_type {};
  2. Kim-J-Smith commented on Feb 10, 2026

    @Kim-J-Smith

    Hope this can be of help to you.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions