C++ 模板重载调用
C++ template overload call
我正在尝试实现一种执行模式,它接受任何函数并用它自己的函数执行它 conditions/preparations。不管这是一件有用的事情,它就是行不通。看来我无法访问 "Execute" 函数的模板重载(在 "main" 中调用)。
具体:为什么我不能调用Execute的重载模板函数?
这是完整的程序:
#include "stdafx.h"
#include <functional>
class TransparentFunctionWrapper
{
public:
virtual void Execute(std::function<void()> executeFunction) = 0;
template<class C>
C Execute(std::function<C(void)> executeFunction) // template-overload of the abstract function which will implicitly call it
{
C ret;
Execute( // calls the abstract function with a lambda function as parameter
[ret, executeFunction](void) -> C // lambda declaraction
{ //
ret = executeFunction; // lambda body
}); //
return ret;
}
};
class ExampleExecutor : public TransparentFunctionWrapper
{
public:
virtual void Execute(std::function<void()> executeFunction)
{
printf("executed before.");
executeFunction();
printf("executed after.");
}
};
void DoStuff() {}
int ReturnStuff() { return -5; }
int main()
{
ExampleExecutor executor;
executor.Execute(DoStuff);
int i = executor.Execute<int>(ReturnStuff); // Why does this not work? ERROR: "type name is not allowed"
getchar();
return 0;
}
注:Visual Studio分
Execute<int>(ReturnStuff) // "int" is marked as Error: type name is not allowed
编译报错
"type 'int' unexpected"
感谢所有愿意帮助的人!
ExampleExecutor::Execute
没有覆盖 TransparentFunctionWrapper::Execute
,它隐藏在 executor.Execute<int>
调用中。
您必须显式调用 TransparentFunctionWrapper::Execute
,因为它被 ExampleExecutor::Execute
隐藏了。这是一种可行的方法:
int i = executor.TransparentFunctionWrapper::Execute<int>(ReturnStuff);
我正在尝试实现一种执行模式,它接受任何函数并用它自己的函数执行它 conditions/preparations。不管这是一件有用的事情,它就是行不通。看来我无法访问 "Execute" 函数的模板重载(在 "main" 中调用)。
具体:为什么我不能调用Execute的重载模板函数?
这是完整的程序:
#include "stdafx.h"
#include <functional>
class TransparentFunctionWrapper
{
public:
virtual void Execute(std::function<void()> executeFunction) = 0;
template<class C>
C Execute(std::function<C(void)> executeFunction) // template-overload of the abstract function which will implicitly call it
{
C ret;
Execute( // calls the abstract function with a lambda function as parameter
[ret, executeFunction](void) -> C // lambda declaraction
{ //
ret = executeFunction; // lambda body
}); //
return ret;
}
};
class ExampleExecutor : public TransparentFunctionWrapper
{
public:
virtual void Execute(std::function<void()> executeFunction)
{
printf("executed before.");
executeFunction();
printf("executed after.");
}
};
void DoStuff() {}
int ReturnStuff() { return -5; }
int main()
{
ExampleExecutor executor;
executor.Execute(DoStuff);
int i = executor.Execute<int>(ReturnStuff); // Why does this not work? ERROR: "type name is not allowed"
getchar();
return 0;
}
注:Visual Studio分
Execute<int>(ReturnStuff) // "int" is marked as Error: type name is not allowed
编译报错
"type 'int' unexpected"
感谢所有愿意帮助的人!
ExampleExecutor::Execute
没有覆盖 TransparentFunctionWrapper::Execute
,它隐藏在 executor.Execute<int>
调用中。
您必须显式调用 TransparentFunctionWrapper::Execute
,因为它被 ExampleExecutor::Execute
隐藏了。这是一种可行的方法:
int i = executor.TransparentFunctionWrapper::Execute<int>(ReturnStuff);