有沒有辦法通過部分綁定第一個/最後一個?可調用對象(例如函數)的參數沒有明確指定其餘參數?函數參數的部分綁定
std::bind()
似乎要求所有參數都被綁定,那些被留下應該綁定到std::placeholders::_1
,_2
,_3
等
是否可以寫一個部分bind_first()
/bind_last()
綁定從第一個/最後一個參數開始,並自動插入佔位符的任何剩餘的未綁定參數的原始順序在他們的原始位置?
有沒有辦法通過部分綁定第一個/最後一個?可調用對象(例如函數)的參數沒有明確指定其餘參數?函數參數的部分綁定
std::bind()
似乎要求所有參數都被綁定,那些被留下應該綁定到std::placeholders::_1
,_2
,_3
等
是否可以寫一個部分bind_first()
/bind_last()
綁定從第一個/最後一個參數開始,並自動插入佔位符的任何剩餘的未綁定參數的原始順序在他們的原始位置?
受這個問題的啓發,我從頭開始寫了自己的預綁定。儘管它看起來與其他人非常相似,但我保證它是原創的:) - 稱之爲融合演進。
雖然它有一個稍微不同的味道。首先,它轉發給它的構造函數,但您可能更喜歡使用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;
}
你肯定?我確信我在無數次的場合都這麼想,總是發現這不是真的。 –
我修復了我給出的實際預綁定的答案,[live example。](http://ideone.com/UUyhah) – user3125280
既不加速也不是標準庫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);
}
有一個'的std :: bind1st'但其棄用。 – Gasim
我擔心這是不可能實現的 – Paranaix
這實際上寫的很微不足道。所有你需要做的就是將綁定的參數存儲在一個'tuple'中,接受'operator()'中的可變數量的參數,通過索引解開元組,然後附加實際的參數。 – Xeo