7

I recently found this nifty snippet on the web - it allows you to bind without having to pass in explicit placeholders:

template <typename ReturnType, typename... Args>
std::function<ReturnType(Args...)> 
easy_bind(ReturnType(*MemPtr)(Args...))
{
  return [=]( Args... args ) -> ReturnType { return (*MemPtr)( args... ); };
}

This version works great with no args:

auto f1 = easy_bind( (std::string(*)(A&,A&))&Worker::MyFn );

later invoked with:

std::string s = f1( *p_a1, *p_a2 );

Question

Is it possible to modify the code to work with anything up to n args, filling 2-n (in this case) with placeholders? For example, this one should have one placeholder:

auto f2 = easy_bind( (std::string(*)(A&,A&))&Worker::MyFn, *p_a1 );     

later invoked with:

std::string s = f2( *p_a2 );

Bonus

Ultimately, it would nice to have something like this (which inserts no placeholders since it will use up the last one), but I don't think it's workable with this implementation (can't pattern-match the signature, I think):

auto f3 = easy_bind( f2, *p_a2 );     

later invoked with:

std::string s = f3();

The bottom line is, it would be nice to have a version of bind where I don't need to put in placeholders - it would be quite useful in generic TMP code.

4

2 回答 2

16

有了索引技巧告诉std::bind你自己的占位符类型的能力,这就是我想出的:

#include <functional>
#include <type_traits>
#include <utility>

template<int I> struct placeholder{};

namespace std{
template<int I>
struct is_placeholder< ::placeholder<I>> : std::integral_constant<int, I>{};
} // std::

namespace detail{
template<std::size_t... Is, class F, class... Args>
auto easy_bind(indices<Is...>, F const& f, Args&&... args)
  -> decltype(std::bind(f, std::forward<Args>(args)..., placeholder<Is + 1>{}...))
{
    return std::bind(f, std::forward<Args>(args)..., placeholder<Is + 1>{}...);
}
} // detail::

template<class R, class... FArgs, class... Args>
auto easy_bind(std::function<R(FArgs...)> const& f, Args&&... args)
    -> decltype(detail::easy_bind(build_indices<sizeof...(FArgs) - sizeof...(Args)>{}, f, std::forward<Args>(args)...))
{
    return detail::easy_bind(build_indices<sizeof...(FArgs) - sizeof...(Args)>{}, f, std::forward<Args>(args)...);
}

活生生的例子。

请注意,我要求函数参数为type 或可转换为easy_bindtype std::function,以便我有明确的可用签名。

于 2013-02-22T13:17:51.063 回答
0

这让我很困扰,因为我不得不在当时不知道参数的情况下绑定一个函数。(此处显示的工厂如何在 C++ 中实现序列化

例如(假设 TSubClass::create 是静态的)

template<typename TFactoryClass, typename TArgs...>
class Factory
{
public:
    template<typename TSubClass>
    void register(int id)
    {
         _map.insert(std::make_pair(id, std::bind(&TClass::create, /*how to give TArgs as placeholders??*/)));
    }
}

相反,我能够用 lambda 表达式替换 std::bind ,而无需使用所有这些辅助类!

template<typename TFactoryClass, typename TArgs...>
class Factory
{
public:
    template<typename TSubClass>
    void register(int id)
    {
         _map.insert(std::make_pair(id, [](TArgs... args) { TSubClass::create(args...); }));
    }
}

作为奖励,您还可以使用此机制“绑定”到构造函数

于 2017-01-01T09:15:05.457 回答