13

残りの引数を明示的に指定せずに、呼び出し可能なオブジェクト (関数など) の最初/最後のn 個の引数を部分的にバインドする方法はありますか?

std::bind()すべての引数がバインドされている必要があるようです。残されるものstd::placeholders::_1は 、など _2にバインドする必要があります。_3

bind_first()最初/最後の引数から始まる部分バインディングの/を記述してbind_last()、残りのバインドされていない引数のプレースホルダーを元の順序で元の位置に自動的に挿入することは可能ですか?

4

3 に答える 3

6

Boost も標準ライブラリもbind、空白を自動的に埋めません。雨の夜を埋める必要がある場合は、そのようなガジェットを自分で作成できます。これは、単純な関数のみの末尾の引数の例です。

#include <tuple>
#include <type_traits>
#include <utility>

template <typename F, typename ...Args> struct trailing_binder;

template <typename R, typename ...Frgs, typename ...Args>
struct trailing_binder<R(Frgs...), Args...>
{
    template <typename ...Brgs>
    trailing_binder(R (*f)(Frgs...), Brgs &&... brgs)
    : the_function(f)
    , the_args(std::forward<Brgs>(brgs)...)
    { }

    template <unsigned int ...I> struct intlist {};

    template <typename ...Brgs>
    typename std::enable_if<sizeof...(Brgs) + sizeof...(Args) == sizeof...(Frgs), R>::type
    operator()(Brgs &&... brgs)
    {
        return unwrap(std::integral_constant<bool, 0 == sizeof...(Args)>(),
                      intlist<>(),
                      std::forward<Brgs>(brgs)...);
    }

private:
    template <unsigned int ...I, typename ...Brgs>
    R unwrap(std::false_type, intlist<I...>, Brgs &&... brgs)
    {
        return unwrap(std::integral_constant<bool, sizeof...(I) + 1 == sizeof...(Args)>(),
                      intlist<I..., sizeof...(I)>(),
                      std::forward<Brgs>(brgs)...);
    }

    template <unsigned int ...I, typename ...Brgs>
    R unwrap(std::true_type, intlist<I...>, Brgs &&... brgs)
    {
        return the_function(std::get<I>(the_args)..., std::forward<Brgs>(brgs)...);
    }

    R (*the_function)(Frgs...);
    std::tuple<Args...> the_args;
};

template <typename R, typename ...Args, typename ...Frgs>
trailing_binder<R(Frgs...), Args...> trailing_bind(R (*f)(Frgs...), Args &&... args)
{
    return trailing_binder<R(Frgs...), typename std::decay<Args>::type...>(f, std::forward<Args>(args)...);
}

使用法:

int f(int a, int b, int c, int d) { return a + b + c + d; }

int main()
{
    auto b = trailing_bind(f, 1);
    return b(3, 8, 13);
}
于 2014-01-19T13:47:36.977 に答える
0

この質問に触発されて、私は独自の prebind をゼロから作成しました。最終的には他のみんなとかなり似たものになりましたが、オリジナルであることを約束します:)-これを収斂進化と呼んでください。

ちょっと変わった味ですが。一つには、それはコンストラクターに転送されますが、使用することを好むかもしれませんstd::decay(ある意味ではより理にかなっていますが、std::refどこにでも書くのは好きではありません)。別の例として、ネストされたプレバインドのサポートを追加したのでprebind(foo, prebind(GetRandomNumber))()、 と同じprebind(foo)(GetRandomNumber())です。

#include <tuple>
#include <type_traits>
using namespace std;

struct pb_tag {}; //use inheritance to mark prebinder structs

//result_of_t will be defined by default in c++1y
template<typename T > using result_of_t = typename result_of<T>::type;
template<typename T> using is_prebinder = is_base_of<pb_tag, typename remove_reference<T>::type >;

//ugly sequence generators for something different
template<int N, int ...S> struct seq : seq<N-1, N, S...> {};
template<int ...S> struct seq<0, S...> {typedef seq type;};

//these three functions are only for nested prebind. they map
//T t -> T t and Prebind<f, T...> -> f(T...)
template<typename T>
auto dispatchee(T&& t, false_type) -> decltype(forward<T>(t)){
    return forward<T>(t);
}

template<typename T>
auto dispatchee(T&& t, true_type) -> decltype(t())
{
    return t();
}

template<typename T>
auto expand(T&& t) -> decltype(dispatchee(forward<T>(t), is_prebinder<T>()))
{
    return dispatchee(forward<T>(t), is_prebinder<T>());
}

template<typename T> using expand_type = decltype(expand(declval<T>()));

//the functor which holds the closure in a tuple
template<typename f, typename ...ltypes>
struct prebinder : public pb_tag
{
    tuple<f, ltypes...> closure;
    typedef typename seq<sizeof...(ltypes)>::type sequence;
    prebinder(f F, ltypes... largs) : closure(F, largs...) {}

    template<int ...S, typename ...rtypes>
    result_of_t<f(expand_type<ltypes>..., rtypes...)>
    apply(seq<0, S...>, rtypes&& ... rargs){
        return (get<0>(closure))(expand(get<S>(closure))... , forward<rtypes>(rargs)...);
    }

    template<typename ...rtypes>
    result_of_t<f(expand_type<ltypes>..., rtypes...)>
    operator() (rtypes&& ... rargs){
        return apply(sequence(), forward<rtypes>(rargs)...);
    }
};

template<typename f, typename ...ltypes>
prebinder<f, ltypes...> prebind(f&& F, ltypes&&... largs)
{
    return prebinder<f, ltypes...>(forward<f>(F), forward<ltypes>(largs)...);
}

ポストバインドにも簡単に変更できます。

使用法は次のようになります。

int g(int a){ return 1 + a; }

int h(){ return 1; }

int i(int a, int b, int c, int d){
    return 1 + a + b + c + d;
}

int main()
{
    //completely bound
    auto a = prebind(g, 1);
    cout << a() << endl;

    //nested bind by reference
    auto b = prebind(g, a);
    cout << b() << endl;
    get<1>(a.closure) = 2;
    cout << b() << endl;

    //bind to prebinder
    auto c = prebind(b);
    cout << c() << endl;

    //nested bind of temp to temp
    auto d = prebind(prebind(g), prebind(h));
    cout << d() << endl;

    //and the one you wanted orginally
    auto e = prebind(i, 1, 1, 1);
    cout << e(1) << endl;

    return 0;
}
于 2014-01-19T13:41:22.680 に答える
0

アドバイスは、バインドを避け、ラムダを使用してメンバー関数への呼び出しをラップすることです。

https://releases.llvm.org/10.0.0/tools/clang/tools/extra/docs/clang-tidy/checks/modernize-avoid-bind.html

于 2020-11-19T11:19:37.223 に答える