如何使用模板函数参数编写包装函数,该功能可以采用超载的成员函数
How do I write a wrapper function with templated function parameters which can take overloaded member functions?
我正在使用旧版代码库,该代码库使用一堆回调来成员函数。作为重构工作的一部分,我正在尝试包装这些回调电话。
我当前的实现正在尝试使用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;
}
相关文章:
- C++:函数外部的超时功能
- 我在 C++ 中创建了一个函数来递归反转字符串,但是之后如何使功能打印一个 endl?
- 设计模式,以避免不必要地添加抽象函数以适应新功能
- 当我链接两个静态C++库时,我可以在两个主函数库中有两个主要功能吗?
- 具有C++迭代器参数的多功能函数
- 错误:调用'strcmp'没有匹配函数(尝试设置显示用户信息功能)
- 我有模板功能.通过使用函数std::for_each,用这个容器中的最大数字替换每个正数
- 是否有一个Windows驱动程序函数可以执行Windows文件api SeFileAttributes的等效功能
- 自定义 {fmt} 格式化函数,具有编译时格式字符串检查功能
- 如何在带有初始值设定项的构造函数中使用 vprintf/cstdarg 功能?
- =删除用户定义的成员功能,除了构造函数,分配运算符C 11
- 如何使用结构内的功能指针调用私有函数
- C 函数调用包装器包含类成员功能作为模板参数
- 您如何在与打开窗口的功能的不同函数中使用Draw函数
- 虚拟静态函数功能的解决方法
- 实现 C++11 委派构造函数功能会导致多个警告
- 复制构造函数功能
- C++构造函数功能
- 如何在保持标准c++函数功能的同时绕过它
- c++思维,内联函数功能