如何使用模板函数参数编写包装函数,该功能可以采用超载的成员函数

How do I write a wrapper function with templated function parameters which can take overloaded member functions?

本文关键字:函数 功能 超载 成员 何使用 参数 包装      更新时间:2023-10-16

我正在使用旧版代码库,该代码库使用一堆回调来成员函数。作为重构工作的一部分,我正在尝试包装这些回调电话。

我当前的实现正在尝试使用variadic模板函数替换/包装绑定调用。

template< typename F, typename T, typename... Args >
auto
my_bind(F fxn, T * obj, Args&&... args)
    -> decltype( boost::bind( fxn, obj, std::forward<Args>(args)... ) )
{
    return boost::bind( fxn, obj, std::forward<Args>(args)... );
}

(实际实现将围绕FXN&amp; obj对象添加包装类,但是我删除了它以提供一个仍然显示问题的最小示例。(

这主要工作,但在FXN对象是一个超载的成员函数的情况下失败。在这种情况下,我会得到"无法推断模板参数'f'"/"无法推断模板参数'f'"(gcc/clang(错误。这有点有道理,因为有多种可能使用的参数类型的功能。

让我感到困惑的是,boost :: bind在成员分辨率上没有问题 - 在没有包装器的原始代码中,我看不出任何错误,绑定良好。示例:

#include <iostream>
#include <boost/bind.hpp> // Boost 1.53
template< typename F, typename T, typename... Args >
auto
my_bind(F fxn, T * obj, Args&&... args)
    -> decltype( boost::bind( fxn, obj, std::forward<Args>(args)... ) )
{
    return boost::bind( fxn, obj, std::forward<Args>(args)... );
}
class Klass {
public:
    void foo( int i ) {
       std::cout << "One param: " << i << "n";
    }
    void foo( int i, int j ) {
       std::cout << "Two param: " << i << " " << j << "n";
    }
    void bar( int const & i ) const {
       std::cout << "Bar One param: " << i << "n";
    }
    int bar( float i, int j ) {
       std::cout << "Bar Two param: " << i << " " << j << "n";
       return j;
    }
};
int main() {
    Klass k;
    auto f1 = boost::bind( &Klass::foo, &k, 1 );
    f1(); // prints "One param: 1"
    auto f2 = boost::bind( &Klass::foo, &k, 1, 2 );
    f2(); // prints "Two param: 1 2"
    //auto f1a = my_bind( &Klass::foo, &k, 1 ); // Compiler error: couldn't deduce template parameter ‘F’
    //auto f2a = my_bind( &Klass::foo, &k, 1, 2 ); // Compiler error:  couldn't deduce template parameter ‘F’
    double a = 1.1;
    int b = 3;
    //auto b1 = my_bind( &Klass::bar, &k, b ); // Should also work with const functions and const parameters
    //auto b2 = my_bind( &Klass::bar, &k, a, 2 ); // As well as non-void return types and parameter conversions
    // As well as any other member function which the underlying sub-function (here boost::bind) can take.
    return 0;
}

我的主要问题:给定一个函数(例如,但不一定限于boost::bind(,该功能能够适当区分超载成员函数的不同版本,是否有一种方法可以创建一个模板包装器函数可以从该函数参数"完美地转发"模板类型 - 也就是说,是否有一种方法可以允许编译器根据(工作(类型扣除子功能(例如boost::bind(进行F类型扣除?

(我确实尝试用variadic预处理器宏来替换my_bind模板函数。这解决了直接问题,但是当我试图将fxn对象包装在模板类型中时,稍后会导致问题。我得到了类似的"不能"解决重载函数"错误"。(

我要定位C 11,如果这有所不同。

您可以尝试强制预期的成员函数的类型:

#include <iostream>
#include <boost/bind.hpp> // Boost 1.53
template<typename T, typename... Args>
auto
my_bind(void (T::*fxn)(Args...), T * obj, Args&&... args)
    -> decltype( boost::bind( fxn, obj, std::forward<Args>(args)... ) )
{
    return boost::bind( fxn, obj, std::forward<Args>(args)... );
}
class Klass {
public:
    void foo( int i ) {
       std::cout << "One param: " << i << "n";
    }
    void foo( int i, int j ) {
       std::cout << "Two param: " << i << " " << j << "n";
    }
};
int main() {
    Klass k;
    auto f1a = my_bind( &Klass::foo, &k, 2 );
    f1a(); // prints One param: 2
    auto f2a = my_bind( &Klass::foo, &k, 2, 3 );
    f2a(); // prints Two param: 2 3
    return 0;
}