C++ 如何为其他类成员函数编写模板包装器方法?
我尝试为具有不同参数的不同函数创建模板包装器。 设置是一个类C++ 如何为其他类成员函数编写模板包装器方法?,c++,class,c++11,templates,c++17,C++,Class,C++11,Templates,C++17,我尝试为具有不同参数的不同函数创建模板包装器。 设置是一个类a,基本实现了两种方法foo和bar。另一类B应包装这些方法并添加新功能 以下链接中的解决方案对于非类函数非常有效: 但是如果我试图从另一个类调用方法,我会得到一个错误 #include <algorithm> #include <functional> #include <iostream> class A { public: void foo(int x) { std:
a
,基本实现了两种方法foo
和bar
。另一类B
应包装这些方法并添加新功能
以下链接中的解决方案对于非类函数非常有效:
但是如果我试图从另一个类调用方法,我会得到一个错误
#include <algorithm>
#include <functional>
#include <iostream>
class A
{
public:
void foo(int x) {
std::cout << "Foo: " << x << std::endl;
}
void bar(int x, float y) {
std::cout << "Bar: " << x << ", " << y << std::endl;
}
};
class B
{
public:
void fooAndMore(int x) {
foobarWrapper(&A::foo, 1);
}
void barAndMore(int x, float y) {
foobarWrapper(&A::bar, 1, 3.5f);
}
template<typename T, typename... Args>
void foobarWrapper(T&& func, Args&&... args)
{
std::cout << "Start!" << std::endl;
std::forward<T>(func)(std::forward<Args>(args)...);
std::cout << "End!" << std::endl;
}
};
int main()
{
B b;
b.fooAndMore(1);
b.barAndMore(2, 3.5f);
}
但我得到的却是:
error C2064: term does not evaluate to a function taking 1 arguments
note: see reference to function template instantiation 'void B::foobarWrapper<void(__thiscall A::* )(int),int>(T &&,int &&)' being compiled
with
[
T=void (__thiscall A::* )(int)
]
error C2064: term does not evaluate to a function taking 2 arguments
note: see reference to function template instantiation 'void B::foobarWrapper<void(__thiscall A::* )(int,float),int,float>(T &&,int &&,float &&)' being compiled
with
[
T=void (__thiscall A::* )(int,float)
]
错误C2064:术语的计算结果不是采用1个参数的函数
注意:请参阅正在编译的函数模板实例化“void B::foobarWrapper(T&&,int&&)”的参考
具有
[
T=void(uu thiscall A::*)(int)
]
错误C2064:项不计算为包含2个参数的函数
注意:请参阅正在编译的函数模板实例化“void B::foobarWrapper(T&&,int&&,float&)”的参考
具有
[
T=void(uu thiscall A::*)(int,float)
]
你知道如何解决这个问题吗?最简单的解决方法是将类
A
的成员函数设置为静态的。
()
试试这个
#include <algorithm>
#include <functional>
#include <iostream>
class A
{
public:
void foo(int x) {
std::cout << "Foo: " << x << std::endl;
}
void bar(int x, float y) {
std::cout << "Bar: " << x << ", " << y << std::endl;
}
};
class B
{
public:
void fooAndMore(int x) {
foobarWrapper(&A::foo, x);
}
void barAndMore(int x, float y) {
foobarWrapper(&A::bar, x, y);
}
template<typename T, typename... Args>
void foobarWrapper(T func, Args&&... args)
{
std::cout << "Start!" << std::endl;
auto caller = std::mem_fn( func); // Newly added lines
caller( A(), args...); // Newly added line
std::cout << "End!" << std::endl;
}
};
int main()
{
B b;
b.fooAndMore(1);
b.barAndMore(2, 3.5f);
}
有关更多详细信息,请参阅此链接感谢您的快速响应。在我的例子中,静态不是一个选项,但我将尝试使用基本实现获取类的实例。Thx:)@boss0r我在更新后的答案中添加了第二个选项。再次感谢你,你帮了我很多:)这个很好,非常完美@Jarod42是的。。。你是对的。我已作出相应的更新。谢谢你的建议,谢谢!我以前没有使用过std::mem_fn。@boss0r欢迎。我还有一个与此主题相关的问题:是否可以获取传递函数的名称?我试过:``auto-funcName=typeid(func.name();标准::cout
class A
{
public:
static void foo(int x) {
^^^^^^
std::cout << "Foo: " << x << std::endl;
}
static void bar(int x, float y) {
^^^^^^
std::cout << "Bar: " << x << ", " << y << std::endl;
}
};
class B
{
public:
void fooAndMore(const A& a_obj, int x) {
foobarWrapper([&]() { return a_obj.foo(x); });
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Args captured to the lambda
}
void barAndMore(const A& a_obj, int x, float y) {
foobarWrapper([&]() { return a_obj.bar(x, y); });
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Args captured to the lambda
}
template<typename T>
void foobarWrapper(T&& func) // no Args needed any more (@credits Jarod42)
{
std::cout << "Start!" << std::endl;
std::forward<T>(func)(); // simply call the func
std::cout << "End!" << std::endl;
}
};
int main()
{
B b;
b.fooAndMore(A{}, 1); // pass a temporary A object
b.barAndMore(A{}, 2, 3.5f);
}
#include <algorithm>
#include <functional>
#include <iostream>
class A
{
public:
void foo(int x) {
std::cout << "Foo: " << x << std::endl;
}
void bar(int x, float y) {
std::cout << "Bar: " << x << ", " << y << std::endl;
}
};
class B
{
public:
void fooAndMore(int x) {
foobarWrapper(&A::foo, x);
}
void barAndMore(int x, float y) {
foobarWrapper(&A::bar, x, y);
}
template<typename T, typename... Args>
void foobarWrapper(T func, Args&&... args)
{
std::cout << "Start!" << std::endl;
auto caller = std::mem_fn( func); // Newly added lines
caller( A(), args...); // Newly added line
std::cout << "End!" << std::endl;
}
};
int main()
{
B b;
b.fooAndMore(1);
b.barAndMore(2, 3.5f);
}
Start!
Foo: 1
End!
Start!
Bar: 2, 3.5
End!