2014-01-19 8 views
12

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

std::bind()_2、残さなければならないものがstd::placeholders::_1にバインドする必要があり、すべて引数がバインドされていることを要求しているようだ、_3など

それは部分的のためにbind_first()/bind_last()を書き込むことができます最初の/最後の引数からバインドし、残りの引数のプレースホルダを元の位置に元の順序で自動的に挿入しますか?

+0

'のstd :: bind1st'が、その非推奨があります。 – Gasim

+0

これは実装が不可能であることを恐れている – Paranaix

+0

これは実際に書くのはかなり簡単です。バインドされた引数を 'tuple'に格納し、可変数のパラメータを' operator() 'で受け取り、タプルをインデックスでアンパックして、実際の引数を追加するだけです。 – Xeo

答えて

0

質問からインスピレーションを得て、私は自分のプレバインドを一から書いた。しかし、それは他の誰とも似ているように見えましたが、私はそれがオリジナルであることを約束します:) - 収束進化と呼んでください。

ただし、少し違った風味があります。一つは、それはコンストラクタに転送されますが、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; 
} 
+0

あなたは確信していますか?私は無数の機会に同じことを考え、常にそれが真実でないことを発見したと確信しています。 –

+0

私は実際のプリバインドを与えるために私の答えを修正しました。[ライブ例](http://ideone.com/UUyhah) – user3125280

6

自動的どちらもブーストも空白で標準ライブラリ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); 
} 
+0

質問に私のコメントリンクをチェックすることをお勧めします。 :) – Xeo

+0

@ Xeo:すべてのコンパイラエラーのリンクを意味しますか? :-)目標は 'bind(f、1)'のようなもので、3つのパラメータで呼び出し可能なものを返すことに注意してください。 –

+0

*質問*では、他の回答ではありません。 :P – Xeo

関連する問題