其他类方法的模板化包装方法

时间:2019-06-14 22:54:37

标签: c++ class c++11 templates wrapper

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

以下链接中的解决方案非常适合非类函数: https://caniuse.com

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

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

有什么办法解决这个问题吗?

提前谢谢!

2 个答案:

答案 0 :(得分:3)

最简单的解决方法是使类A的成员函数为staticSee online

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

否则,您需要传递类A的实例以在foobarWrapper函数中调用其成员函数。使用lambda可以将它们打包到可调用的func并传递到foobarWrapper

See online

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

答案 1 :(得分:1)

尝试一下

#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