绑定函数的第一个参数而不知道它的参数 [英] bind first argument of function without knowing its arity

查看:132
本文介绍了绑定函数的第一个参数而不知道它的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个函数 BindFirst ,它绑定函数的第一个参数,而不必使用std明确地知道/声明函数的arity: :占位符。我希望客户端代码看起来像这样。

  #include< functional> 
#include< iostream>

void print2(int a,int b)
{
std :: cout<< a<<的std :: ENDL;
std :: cout<< b<的std :: ENDL;
}

void print3(int a,int b,int c)
{
std :: cout<< a<<的std :: ENDL;
std :: cout<< b<的std :: ENDL;
std :: cout<< c<的std :: ENDL;
}

int main()
{
auto f = BindFirst(print2,1); // std :: bind(print2,1,std :: placeholders :: _ 1);
auto g = BindFirst(print3,1); // std :: bind(print3,1,std :: placeholders :: _ 1,std :: placeholders :: _ 2);
f(2);
g(2,3);
}

任何想法 BindFirst 可以实现吗?

解决方案

在$ C $ 11中:

  #include< type_traits> 
#include< utility>

模板< typename F,typename T>
结构绑定
{
F f; T t;
模板< typename ... Args>
auto operator()(Args&& ... args)const
- > decltype(f(t,std :: forward< Args>(args)...))
{
return f(t,std :: forward< Args>(args)...);
}
};

模板< typename F,typename T>
binder< typename std :: decay< F> :: type
,typename std :: decay< T> :: type> BindFirst(F& f,T& t)
{
return {std :: forward F(f),std :: forward< T>(t)};
}

DEMO 1

在C ++ 14中:

  #include< utility> 

模板< typename F,typename T>
auto BindFirst(F&& f,T& t)t
{
return [f = std :: forward F(f),t = std :: forward< T>]
(auto& ... args)
{return f(t,std :: forward< decltype(args)>(args)...); };
}

DEMO 2


I'd like to have a function BindFirst that binds the first argument of a function without me having to explicitly know/state the arity of the function by using std::placeholders. I'd like the client code to look something like that.

#include <functional>
#include <iostream>

void print2(int a, int b)
{
    std::cout << a << std::endl;
    std::cout << b << std::endl;
}

void print3(int a, int b, int c)
{
    std::cout << a << std::endl;
    std::cout << b << std::endl;
    std::cout << c << std::endl;
}

int main()
{ 
    auto f = BindFirst(print2, 1); // std::bind(print2, 1, std::placeholders::_1);
    auto g = BindFirst(print3, 1); // std::bind(print3, 1, std::placeholders::_1, std::placeholders::_2);
    f(2);
    g(2,3);
}

Any ideas how BindFirst could be implemented?

解决方案

In C++11:

#include <type_traits>
#include <utility>

template <typename F, typename T>
struct binder
{
    F f; T t;
    template <typename... Args>
    auto operator()(Args&&... args) const
        -> decltype(f(t, std::forward<Args>(args)...))
    {
        return f(t, std::forward<Args>(args)...);
    }
};

template <typename F, typename T>
binder<typename std::decay<F>::type
     , typename std::decay<T>::type> BindFirst(F&& f, T&& t)
{
    return { std::forward<F>(f), std::forward<T>(t) };
}

DEMO 1

In C++14:

#include <utility>

template <typename F, typename T>
auto BindFirst(F&& f, T&& t)
{
    return [f = std::forward<F>(f), t = std::forward<T>(t)]
           (auto&&... args)
           { return f(t, std::forward<decltype(args)>(args)...); };
}

DEMO 2

这篇关于绑定函数的第一个参数而不知道它的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆