使用可调用参数在callable上重载

时间:2013-02-12 02:57:56

标签: c++ templates lambda functional-programming template-meta-programming

我正在尝试将C#代码翻译成C ++:

void SomeCall(Action action)
{
   // do things like action();  

}

void SomeCall(Action<Action> action)
{
   // define some action1   
   // do things like action(action1);   

}

相当于SomeCall的C ++应该能够使用std :: function以及内联和大纲相同签名的C ++ lambda。

在浏览了很多关于C ++ std :: function和lambdas上的重载的SO问题后,似乎就是这样 答案应该如下:

template<typename Func>
enable_if<Func is something callable>
void SomeCall(Func&& action)
{
 ...
}

template<typename Func>
enable_if<Func is something callable taking another callable as the parameter>
void SomeCall(Func&& action)
{
 ...

}

你能帮我填空吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用标准重载:

void Fn( std::function<int(int)>& fn )
{
}

void Fn( std::function<int(float)>& fn )
{
}

如果这是不可接受的,你将不得不对模板元编程进行大量研究,以使enable_if能够按照你想要的方式工作,这是可以想象的最悲观的编程形式。尽管如此,你可以尝试从Andrei Alexandrescu的Modern C ++ Design开始。