定义可以执行对象方法和独立函数的函数时出现问题
Problem with defining a function which can execute object's methods and standalone functions
我坚持定义一个函数,该函数可以使用模板执行独立的函数和对象方法。
template <typename Func, typename ...Args>
void execute(Func&& f, Args&&... args)
{
cout << "execute1" << endl;
std::forward<decltype(f)>(f)(std::forward<Args>(args)...);
}
template <typename Func, typename Obj, typename ...Args>
void execute(Func&& fun, Obj&& obj, Args&&... args)
{
cout << "execute2" << endl;
std::forward<decltype(obj)>(obj).fun(args...);
}
void fun()
{
cout << "in fun()" << endl;
}
class A
{
public:
void method()
{
cout << "in method()" << endl;
}
};
int main()
{
A a;
execute(fun);
execute(&A::method, std::move(a));
return 0;
}
现在,如果我传递 fun() 函数作为参数,它会通过第一次重载正确执行。
当我传递对象的方法和应该调用此方法的对象时,出现以下错误:
main.cpp: In instantiation of ‘void execute(Func&&, Obj&&, Args&& ...) [with Func = void (A::*)(); Obj = A; Args = {}]’:
main.cpp:20:38: error: ‘class A’ has no member named ‘fun’
std::forward<decltype(obj)>(obj).fun(args...);
我是模板新手,所以请原谅:)
几个问题:
两个重载对于execute(&A::method, std::move(a));
都是可行的,第一个是更好的匹配。所以你的错误。
您可以使用 SFINAE 丢弃无效重载:
template <typename Func, typename ...Args>
auto execute(Func&& f, Args&&... args)
-> decltype(std::forward<Func>(f)(std::forward<Args>(args)...))
{
cout << "execute1" << endl;
std::forward<Func>(f)(std::forward<Args>(args)...);
}
template <typename Func, typename Obj, typename ...Args>
auto execute(Func&& fun, Obj&& obj, Args&&... args)
-> decltype((std::forward<Obj>(obj).*fun)(std::forward<Args>(args)...);)
{
cout << "execute2" << endl;
(std::forward<Obj>(obj).*fun)(std::forward<Args>(args)...);
}
您可能会注意到,我还修复了使用方法指针(.*
或->*
)进行方法调用的语法。 第二个重载仅处理引用对象,而不处理(智能)指针。将添加其他重载来处理这个问题。
在 std 中,我们有std::invoke
(C++17) 这样做,甚至更多。
相关文章:
- 类C++中的函数问题(LNK2019和LNK1120错误)
- 返回不停止函数,递归函数问题?(编程练习,动态规划,Levenshtein 回溯)
- 变分模板递归构造函数问题
- C++函数问题中的数组分配
- C++中嵌套在另一个 LinkedList 中的 LinkedList 整数上的回调函数问题
- C++重载构造函数问题
- 二叉搜索树析构函数问题
- C++ vector<pair<int,int>> std::all_of() 函数问题(重定向到头文件)
- 迷宫构造函数问题 [线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)]
- C++函数问题中的 IF 语句
- 函数问题,未定义的引用错误
- C++ 指针无效函数问题
- C++函数问题 = 帮助我理解基本概念
- 由于基于接口的编程,遇到 C++ 虚拟模板函数问题
- C++二进制树递归析构函数问题
- 构造函数问题<无法读取内存>
- 构造函数问题
- C 时间函数问题
- 另一个类中的类的构造函数问题
- C与C++函数问题