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

时间:2018-10-24 07:26:56

标签: templates lambda c++17 variadic-templates std-function

我这里有几个类,第一个是存储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
     

节目退出:

Code (0)

1 个答案:

答案 0 :(得分:2)

您正试图在名为operator()(int, int)的{​​{1}}实例上调用Functor。相反,您需要命名此结构callback_的数据成员。像这样:

call_back