可变参数模板,std :: function和lambdas作为类成员

弗朗西斯·库格勒

我这里有几个类,第一个是存储std::function类型的类。

template<typename RES_TYPE, typename... ARG_TYPES>
struct Functor {
    std::function<RES_TYPE( ARG_TYPES... )> call_back;
};

我还有另一个类,它将注册来自函数对象,函数指针或lambda的传入函数,并在上面存储该类的实例,并具有另一个将调用它的成员函数。

template<typename RES_TYPE, typename... ARG_TYPES>
class Driver {
private:
    Functor<RES_TYPE, ARG_TYPES...> callback_;

public:
    Driver() = default;
    ~Driver() = default;

    void register_callback( const Functor<RES_TYPE, ARG_TYPES...> &func ) {
        callback_ = func;
    }

    RES_TYPE call_back( ARG_TYPES... args ) {
        return callback_( args... );
    }    
};

我试图将语法正确地传递到试图调用该函数的值的位置。我的主看起来像这样:

int main() {    
    Functor<int, int, int> func;
    auto lambda = []( int a, int b ) { return a + b; };
    func.call_back = lambda;

    Driver<int, int, int> driver;
    driver.register_callback( func );
    std::cout << driver.call_back( 3, 5 ) << '\n';

    return 0;
}

我似乎无法全神贯注于语法中缺少的内容。我已经尝试了其他各种方法,但是使用此当前设置,是从Visual Studio 2017 CE中收到此编译器错误:

1>------ Build started: Project: Temp, Configuration: Debug Win32 ------
1>main.cpp
1>c:\...\main.cpp(62): error C2064: term does not evaluate to a function taking 2 arguments
1>c:\...\main.cpp(62): note: while compiling class template member function 'RES_TYPE Driver<RES_TYPE,int,int>::call_back(int,int)'
1>        with
1>        [
1>            RES_TYPE=int
1>        ]
1>c:\...\main.cpp(77): note: see reference to function template instantiation 'RES_TYPE Driver<RES_TYPE,int,int>::call_back(int,int)' being compiled
1>        with
1>        [
1>            RES_TYPE=int
1>        ]
1>c:\...\main.cpp(75): note: see reference to class template instantiation 'Driver<int,int,int>' being compiled
1>Done building project "Temp.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我对编译器告诉我的想法有所了解,但是我似乎无法正确获取语法来存储此lambda,然后能够调用它。我试图使这些类尽可能通用,以便它可以接受lambda,函数对象或函数指针。


我的代码现在看起来像这样,并且运行良好!

template<typename RES_TYPE, typename... ARG_TYPES>
struct Functor {
    std::function<RES_TYPE( ARG_TYPES... )> func_;
};

template<typename RES_TYPE, typename... ARG_TYPES>
class Driver {
private:
    Functor<RES_TYPE, ARG_TYPES...> functor;

public:
    Driver() = default;
    ~Driver() = default;

    void register_callback( const Functor<RES_TYPE, ARG_TYPES...> &func ) {
        functor = func;
    }

    RES_TYPE call_back( ARG_TYPES... args ) {
        return functor.func_( args... );
    }    
};

int main() {    
    Functor<int, int, int> func;
    auto lambda = []( int a, int b ) { return a + b; };
    func.func_ = lambda;

    Driver<int, int, int> driver;
    driver.register_callback( func );
    int a = 3;
    int b = 5;
    std::cout << driver.call_back( a, b ) << '\n';
    std::cout << driver.call_back( 7, 5 ) << '\n';

    Functor<void, std::string, std::string> func2;
    auto lambda2 = []( std::string str1, std::string str2 ) {
        str1 = str1 + " " + str2;
        std::cout << str1 << '\n';
    };

    Driver <void, std::string, std::string> driver2;
    func2.func_ = lambda2;
    std::string str1 = "Hello";
    std::string str2 = "World";
    driver2.register_callback( func2 );
    driver2.call_back( str1, str2 );
    driver2.call_back( "Generic", "Programming" );

    return 0;
}

预期产出:

8
12
Hello World
Generic Programming

实际输出:

8
12
Hello World
Generic Programming

Progam出口:

Code (0)
润滑脂

您正在尝试调用名为operator()(int, int)Functor实例callback_相反,您需要命名此struct的数据成员call_back像这样:

RES_TYPE call_back( ARG_TYPES... args ) {
    return callback_.call_back( args... );
}

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章