相关疑难解决方法(0)

使用两个以上的arg推导std :: function

我想知道为什么std::function只知道两个参数的功能.我写了一些运行良好的代码,但是有很多限制.任何反馈欢迎.特别是,我怀疑我正在重新发明轮子.

我的代码是关于ideone的,我将参考它.

例如,我可以用以下方式描述main:

function_type_deducer(main).describe_me();
// Output: I return i and I take 2 arguments.  They are of type:  i PPc
Run Code Online (Sandbox Code Playgroud)

(其中'i'表示'int','PPc'表示指向指针指向char的指针)

Standard std::function不适用于具有两个以上args的函数(请参阅我的代码的最后两行),但此代码可以使用(示例代码演示了三个arg函数).也许我的设计应该在标准库中使用!我定义typedef tuple<Args...> args_as_tuple;存储所有args,而不仅仅是前两个参数类型.

主要技巧是这个函数的推论:

template<class T, class... Args>
auto function_type_deducer(T(Args...)) -> Function__<T, Args...> {
        return Function__<T, Args...> {};
}
Run Code Online (Sandbox Code Playgroud)

限制:

  • 它不适用于lambdas.这不会编译function_type_deducer([](){}).describe_me();
  • 它没有注意到有差异小x和y,如y需要string&,哪里x需要string.(std :: function也没有注意到这一点)

关于如何修复其中任何一个的任何想法?我重新改造了车轮吗?

c++ templates c++11 template-argument-deduction

3
推荐指数
1
解决办法
450
查看次数