既指向成员函数的指针又指向const成员函数的指针的函数 [英] Function taking both pointer to member-function and pointer to const member-function

查看:101
本文介绍了既指向成员函数的指针又指向const成员函数的指针的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码库:

template <typename Type>
class SomeClass {
public:
    template <typename ReturnType, typename... Params>
    void register_function(const std::pair<std::string, ReturnType (Type::*)(Params...)> fct) {
        auto f = [fct](Params... params) -> ReturnType { return (Type().*fct.second)(std::ref(params)...); }
        // ...
    }
};

当我将指针传递给成员函数(非const)时,此方法有效。
但是,如果我想传递一个指向const成员函数的指针,则会导致编译错误,我必须复制以上函数才能获得此代码:

This works when I pass a pointer to a member-function (non-const). However, if I want to pass a pointer to a const member-function, it results in a compile error and I must duplicate the above function to get this code:

template <typename Type>
class SomeClass {
public:
    template <typename ReturnType, typename... Params>
    void register_function(const std::pair<std::string, ReturnType (Type::*)(Params...)> fct) {
        auto f = [fct](Params... params) -> ReturnType { return (Type().*fct.second)(std::ref(params)...); }
        // ...
    }

    template <typename ReturnType, typename... Params>
    void register_function(const std::pair<std::string, ReturnType (Type::*)(Params...) const> fct) {
        auto f = [fct](Params... params) -> ReturnType { return (Type().*fct.second)(std::ref(params)...); }
        // ...
    }
};

现在,我既可以传递const成员函数,也可以传递非const成员函数。但是,现在,代码是重复的,并且降低了可维护性。

Now, I can pass both const-member-functions and non-const-member-functions. But, now, the code is duplicate and maintainability is reduced.

是否可以将这两个函数合并为一个既包含const成员函数又包含非成员函数的函数。 const-member-functions?

Is there a way to merge these two functions into a function taking both const-member-functions and non-const-member-functions?

重要说明:我必须真正将指针函数用作参数(无std :: function)。

Important note: I must really take a pointer function as parameter (no std::function).

编辑:我添加了更多代码。
在函数内部,我构建了一个与成员函数签名匹配的闭包(相同的返回类型和参数)。
此闭包将被存储并在以后用于反射(更多内容

I've added a little bit more code. Inside the functions, I build a closure matching the member function signature (same return types and params). This closure will be stored and used later for making reflection (more here)

推荐答案

您可以编写类型特征,基于该特征可以告诉您如果某些 MF Type 类型的成员函数指针:

You could write a type trait, based on which will tell you if some MF is a pointer-to-member function on Type:

template <typename C, typename T>
struct is_pointer_to_member_helper : std::false_type { };

template <typename C, typename T>
struct is_pointer_to_member_helper<C, T C::*> : std::is_function<T> { };

template <typename C, typename T>
struct is_pointer_to_member : is_pointer_to_member_helper<C,
                                  std::remove_cv_t<T>
                              > { };

并使用它来确保只获得其中之一:

And use it to ensure that you only get one of those:

template <typename Type>
class SomeClass {
public:
    template <typename MF>
    std::enable_if_t<is_pointer_to_member<Type, MF>::value>
    register_function(const std::pair<std::string, MF> fct) 
    {
        auto f = [fct](auto&&... params) {
            return (Type{}.*fct.second)(std::forward<decltype(params)>(params)...);
        };

        // ...
    }
};

这篇关于既指向成员函数的指针又指向const成员函数的指针的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆