如何为其他类成员函数编写模板包装器方法?

老板0r

我尝试为具有不同参数的不同功能创建模板包装器。该设置是一类,A具有两个方法foo的基本实现bar另一个类B应包装这些方法并添加新功能。

以下链接中的解决方案非常适用于非类函数:c ++ 11:模板化包装器函数

但是,如果我尝试从另一个类调用方法,则会收到错误消息。

#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);
}

我期望这样的事情:

Start!
Foo: 1
End!
Start!
Bar: 1, 3.5
End!

但是我得到了:

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)
    ]

任何想法如何解决这个问题?

维卡斯(Vikas Awadhiya)

尝试这个,

#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!

有关更多详细信息,请参见此链接std :: mem_fn

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 C++ 中为覆盖某些成员并继承其他成员的类编写包装器

其他成员函数的通用“成员函数”包装器?

如何为特定于类型的访问函数编写通用模板化包装器?

如何为类编写“获取”方法模板

成员函数模板推导指南或其他方法,以使编译器知道如何调用函数

如何为 Kotlin Coroutines Flow 编写扩展函数/包装器?

如何为某些模板类型禁用类成员函数

如何为C ++方法编写目标C包装器?

如何让方法访问其他模板类实例的私有成员?

如何为包装需要2个参数的ac函数的unique_ptr类成员创建自定义删除器?

如何编写需要回调的C函数的C ++包装器类方法?

如何为 axios 请求编写包装函数?

C ++:通用函数包装器类作为非模板类的成员

如何在SWIG中包装可变参数模板类的可变参数模板成员函数?

如何为其他方法封装类方法

困惑于如何为重构为其他类的类编写测试

调用其他类的成员函数

如何为带有继承的C ++类编写ac包装器

包装成员函数调用的模板化类的行为

如何围绕表达式模板编写第三方库包装器类

如何通过std :: unique_ptr成员调用其他类的const成员函数

如何使用可变参数模板编写通用函数包装器

具有单个参数模板的成员函数包装器?

如何访问其他模板类实例的私有成员?

如何为包装函数的类型编写任意实例?

如何为使用包装的Azure函数编写单元测试?

如何定义专用模板类的成员函数?

如何编写基于Django类的视图以用于其他模板的表单显示和提交?

如何为模板类创建“工厂函数”?