Templates 可变模板、std::function和lambda作为类成员
这里我有几个类,第一个是存储Templates 可变模板、std::function和lambda作为类成员,templates,lambda,c++17,variadic-templates,std-function,Templates,Lambda,C++17,Variadic Templates,Std Function,这里我有几个类,第一个是存储std::functiontype的类 template<typename RES_TYPE, typename... ARG_TYPES> struct Functor { std::function<RES_TYPE( ARG_TYPES... )> call_back; }; 实际产出: 程序出口: 您正在尝试调用名为回调的实例上的操作符()(int,int)。相反,您需要命名此结构的数据成员call\u back。像这样: R
std::function
type的类
template<typename RES_TYPE, typename... ARG_TYPES>
struct Functor {
std::function<RES_TYPE( ARG_TYPES... )> call_back;
};
实际产出:
程序出口:
您正在尝试调用名为
回调的实例上的操作符()(int,int)
。相反,您需要命名此结构的数据成员call\u back
。像这样:
RES_TYPE call_back( ARG_TYPES... args ) {
return callback_.call_back( args... );
}
非常感谢你:我是新的,我是亲密的;我错过了一些琐碎的事情,但有时有第二双眼睛在周围总是好的!是的,我知道正向参考;但是我们首先要解决的是编译器错误!一个名为callback
的数据成员有一个名为callback
的数据成员需要调用,这确实是很容易出错的:)是的,我同意;我将修正一些命名约定!只是想让代码按预期工作!我忽略了方法call\u back
是在一个非推断上下文中。。。这意味着ARG\u TYPE&&…
没有命名通用引用,而是命名一个右值引用,因为在类构造时已经指定了模板参数类型。是我的错。我会调整答案的!最好的办法是你也把你的问题删减到原来的版本。
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;
}
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 ==========
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)
RES_TYPE call_back( ARG_TYPES... args ) {
return callback_.call_back( args... );
}