如何用变量模板c++泛化这个函数。

问题描述 投票:0回答:1

我有以下功能。它将两个绑定的 T0T1 捆绑到一个 tuple<T0,T1>

其功能如下

template<typename T0, typename T1>
typename RxBinding<std::tuple<T0,T1>>::Ptr
Combine(RxBinding<T0>::Ptr b0, RxBinding<T1>::Ptr b1)
{
    using Tuple = std::tuple<T0,T1>;
    RxBinding<Tuple>::Ptr binding = makeValueBinding(std::make_tuple(b0->Get(),b1->Get()));

    // Break the reference cycle. 
    auto bindingWeak = std::weak_ptr<RxBinding<Tuple>>(binding);

    auto s0 = b0->Subscribe([bindingWeak,b1](T0 const & v0){
        auto b = bindingWeak.lock();
        if(b)
            b->Update(std::make_tuple(v0,b1->Get()));
    });

    auto s1 = b1->Subscribe([bindingWeak,b0](T1 const & v1){
        auto b = bindingWeak.lock();
        if(b)
            b->Update(std::make_tuple(b0->Get(),v1));
    });

    auto sN =  binding->Subscribe([b0,b1](std::tuple<T0,T1> const & t){
        b0->Update(std::get<0>(t));
        b1->Update(std::get<1>(t));
    });

    binding->CleanupWith << s0 << s1 << sN;

    return binding;
}

不要太担心什么是绑定。假设它们可以工作。我正在寻找一种模式,使用C++11的变量模板来概括这个问题,这样我就可以有N个绑定作为输入,而不是只有两个,并将它们转换为一个绑定?

template <typename ...T>
typename RxBinding<std::tuple<T...>>::Ptr
Combine( RxBinding<T>::Ptr args...) /* is this possible ?? */
{
    using Tuple = std::tuple<T...>;
    auto binding = makeValueBinding(std::make_tuple( /* what do do here with args ?? */ ));

    // Break the reference cycle. 
    RxBinding<Tuple>::Ptr bindingWeak = std::weak_ptr<RxBinding<Tuple>>(binding);

    // Make N subscriptions b0,b1,....bN with the weak reference above

    /* What to do here ?? */

    // Make the final subscription

    auto sN = binding->Subscribe([](std::tuple<T...> const & t){
        // Update the N bindings.

        /* what to do here ? */

    });

    // Add all subscriptions to the cleanup on the final binding
    /* not sure what to do here */

    return binding;
}
c++ c++11 variadic-templates
1个回答
1
投票

来自 RxBinding<T>::Ptr T 不能被推导出来,因为它是一个非推导的上下文,因为嵌套类型(见例1)在非推导的上下文下的 cppreference栓例),所以原来的例子不应该用论证演绎来工作。 考虑到有 typename RxBinding<Ts>::Ptr ...args 的工作方式与之前的一样(注意语法中的 ... 前的参数名称)。) 我将变量类型模板改为 Ts 而不是 T,以更好地体现它的可变性。

有了 auto binding = makeValueBinding(std::make_tuple( /* what do do here with args ?? */ )); 你可以用 扩容包 有模有样 args->Get()因此,最后一行将是 auto binding = makeValueBinding(std::make_tuple(args->Get()...));.

变量的创建 s0, s1,等等都不是小事,所以我在最后再讲。

要进行最后的认购,你需要使用一个辅助函数来扩展元组。

template<typename ...ArgTypes, typename ...Ts, std::size_t ...Ns>
void FinalSubscribeHelper(
    std::tuple<ArgTypes...> const &args,
    std::tuple<Ts...> const &t,
    std::index_sequence<Ns...>
)
{
    // using C++17's fold expressions (https://en.cppreference.com/w/cpp/language/fold)
    ((std::get<Ns>(args)->Update(std::get<Ns>(t))), ...); // we use the comma operator for expansion
    return;

    // using array initializers for C++11
    using ArrayT = int[sizeof...(ArgTypes)];
    ArrayT{
        ((
            std::get<Ns>(args)->Update(std::get<Ns>(t)) // this is the pattern
        ), 0)...
    };
    return;
}

所以最后的认购是

auto sN = binding->Subscribe([=](std::tuple<Ts...> const &t){
    // Update the N bindings.
    FinalSubscribeHelper(std::make_tuple(args...), t, std::make_index_sequence<sizeof...(Ts)>{});
});

为了将所有的订阅添加到清理中,你需要另一个辅助函数。

template<typename BindingT, typename ...STs, typename SNT, std::size_t ...Ns>
void CleanupHelper(
    BindingT const &binding,
    std::tuple<Ts...> const &s,
    SNT const &sN
    std::index_sequence<Ns...>
)
{
    // using C++17's fold expressions (https://en.cppreference.com/w/cpp/language/fold)
    (binding->CleanupWith << ... << std::get<Ns>(s)) << sN;
    return;

    // using array initializers for C++11
    /*
    this only works if
    binding->CleanupWith << s0 << s1 << sN;
    is equivalent to
    binding->CleanupWith << s0;
    binding->CleanupWith << s1;
    binding->CleanupWith << sN;
    */
    using ArrayT = int[sizeof...(ArgTypes)];
    ArrayT{
        ((
            binding->CleanupWith << std::get<Ns>(s)
        ), 0)...
    };
    binding->CleanupWith << sN;
    return;
}

所以最后的清理工作是CleanupHelper(binding, s, sN, std::make_index_sequence<sizeof...(Ts)>{});.

现在回去创建 s. 要创建回调,我假设你想要 Update 称为 b->Update(std::make_tuple(/* bM->Get() with M = 0, 1, 2, ..., I-1 */, vI, /* bM->Get() with M = I+1, I+2, ..., N-1 */));. 为此,你需要两个索引序列,一个来自于 0I-1 和一个来自 I+1N-1. 为此,让我们创建一些类型的别名,使所需的 std::index_sequence's.

template<std::size_t Offset, typename T>
struct AddOffset;

template<std::size_t Offset, std::size_t ...Ns>
struct AddOffset<Offset, std::index_sequence<Ns...>>
{
    using type = std::index_sequence<(Ns + Offset)...>;
};

template<std::size_t Offset, typename T>
using AddOffsetT = typename AddOffset<Offset, T>::type;

// this creates a std::index_sequence with the values
// Start, Start+1, Start+2, ..., End-1
template<std::size_t Start, std::size_t End>
using MakeIndexSequenceInRange = AddOffsetT<Start, std::make_index_sequence<End - Start>>;

要创建 s 你将需要一些辅助函数。

template<typename BindingT, typename ...ArgTypes, typename VT, std::size_t ...Ns, std::size_t ...Ms>
void SubscribeCallbackHelper(
    BindingT const &b,
    std::tuple<ArgTypes...> const &args,
    VT const &v,
    std::index_sequence<Ns...>,
    std::index_sequence<Ms...>
)
{
    b->Update(std::make_tuple(std::get<Ns>(args)->Get()..., v, std::get<Ms>(args)->Get()...));
}

template<typename BindingWeakT, typename ...ArgTypes, std::size_t ...Ns>
auto CreateS(
    BindingWeakT const &bindingWeak,
    std::tuple<ArgTypes...> const &args,
    std::index_sequence<Ns...>
) -> decltype(std::make_tuple(std::get<Ns>(args)->Subscribe(std::declval<void(*)(ArgTypes const &)>())...))
// I'm not sure this decltype will work, if you have C++14 you should be able to just use auto as a return type
{
    return std::make_tuple(
        std::get<Ns>(args)->Subscribe([bindingWeak, args](ArgTypes const &v) {
            auto b = bindingWeak.lock();
            if (b)
                SubscribeCallbackHelper(b, args, v, MakeIndexSequenceInRange<0, Ns>{}, MakeIndexSequenceInRange<Ns+1, sizeof...(ArgTypes)>{});
        })
    );
}

所以创建 s 将是

auto s = CreateS(bindingWeak, std::make_tuple(args...), std::make_index_sequence<sizeof...(Ts)>{});
© www.soinside.com 2019 - 2024. All rights reserved.