繁体   English   中英

可变参数模板和 std::bind

[英]Variadic templates and std::bind

鉴于以下模板化函数,我如何更改它以利用可变参数模板? 也就是说,用可变参数代替 P1 和 P2 替换 std::bind 占位符? 目前我每个元都有这些函数之一,元零没有 P 参数,最高元 9 有 P1 到 P9 参数。 如果可能的话,我希望将其折叠为一个函数。

template<typename R, typename T, typename U, typename P1, typename P2>
void Attach(R (T::*f)(P1, P2), U p)
{
    AttachInternal(p, std::bind(f, 
                                p.get(), 
                                std::placeholders::_1, 
                                std::placeholders::_2));
}

您可以(部分)特化std::is_placeholder以实现自定义模板的特化。 这样,您可以通过通常的int_sequence技术引入占位符生成器。

来自 [func.bind.isplace]/2

如果Tstd::placeholders::_J的类型,则实现应提供具有BaseCharacteristic integral_constant<int, J>BaseCharacteristic的定义,否则它应具有BaseCharacteristic integral_constant<int, 0>BaseCharacteristic 程序可以为用户定义的类型T此模板,使其具有BaseCharacteristic integral_constant<int, N>BaseCharacteristic ,其中N > 0以指示T应被视为占位符类型。

通常的int_sequence

#include <cstddef>

template<int...> struct int_sequence {};

template<int N, int... Is> struct make_int_sequence
    : make_int_sequence<N-1, N-1, Is...> {};
template<int... Is> struct make_int_sequence<0, Is...>
    : int_sequence<Is...> {};

is_placeholder的自定义占位符模板和专业化:

template<int> // begin with 0 here!
struct placeholder_template
{};

#include <functional>
#include <type_traits>

namespace std
{
    template<int N>
    struct is_placeholder< placeholder_template<N> >
        : integral_constant<int, N+1> // the one is important
    {};
}

我不确定在哪里介绍1 ; 我考虑的地方都不是最佳的。

用它来写一些活页夹:

template<class Ret, class... Args, int... Is>
void my_bind(Ret (*p)(Args...), int_sequence<Is...>)
{
    auto x = std::bind(p, placeholder_template<Is>{}...);
    x( Args(42)... );
}

template<class Ret, class... Args>
void my_bind(Ret (*p)(Args...))
{
    my_bind(p, make_int_sequence< sizeof...(Args) >{});
}

活页夹的使用示例:

#include <iostream>

void foo(double, char, int) { std::cout << __PRETTY_FUNCTION__ << "\n"; }
void bar(bool, short) { std::cout << __PRETTY_FUNCTION__ << "\n"; }

int main()
{
    my_bind(foo);
    my_bind(bar);
}

我想提出一个更简单的解决方案来将成员函数绑定到可变数量的占位符:

template<typename R, typename T, typename U, typename... Args>
std::function<R(Args...)> Attach(R (T::*f)(Args...), U p)
{
    return [p,f](Args... args)->R { return (p->*f)(args...); };
};

一个简单的用法示例如下所示

class CrazyAdd
{
public:

    CrazyAdd(double val)
    : m_crazyPart(val)
    {}

    double add(double a, double b)
    {
        return a+b+m_crazyPart;
    }

private:
    double m_crazyPart;
};

void main() {

    CrazyAdd myAdd(39);

    // Create a function object that is bound to myAdd
    auto f = Attach(&CrazyAdd::add, &myAdd);

    // Call the function with the parameters    
    std::cout << f(1,2) << std::endl;   // outputs 42
}

我个人认为这是 Scott Meyer 推荐 lambdas 而不是 std::bind 的另一个很好的例子。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM