实施';拉链';用C++; 我尝试编写一个演示程序来实现Haskell的代码> zip 函数,在C++中使用继续,并且我实现了 fMAP和 > >。我的代码如下所示: template <typename Upstream, typename Other, typename Zip> struct F_zip; template <typename T> struct F { template <typename Other, typename Zip> F_zip<T, Other, Zip> zip(const Other& other, const Zip& z) const { return {*static_cast<T const*>(this), other, z}; } }; template <typename X> struct F_id : F<F_id<X>> { const X x; F_id(const X& x) : x(x) {} template <typename Callback> void operator()(Callback&& callback) const { callback(x); } }; template <typename Upstream, typename Other, typename Zip> struct F_zip : F<F_zip<Upstream, Other, Zip>> { Upstream upstream; Other other; Zip zip; F_zip(const Upstream& upstream, const Other& other, const Zip& zip) : upstream(upstream), other(other), zip(zip) {} template <typename Callback> void operator()(Callback&& callback) const { upstream([=](auto a) { other([=, &a](auto b) { callback(zip(a, b)); }); }); } }; void test_zip() { F_id(10) .zip(F_id(20), [](int x, int y) { return std::make_tuple(x, y); })([](atuo x) { auto [a, b] = x; printf("(%d, %d)\n", a, b); }); }

实施';拉链';用C++; 我尝试编写一个演示程序来实现Haskell的代码> zip 函数,在C++中使用继续,并且我实现了 fMAP和 > >。我的代码如下所示: template <typename Upstream, typename Other, typename Zip> struct F_zip; template <typename T> struct F { template <typename Other, typename Zip> F_zip<T, Other, Zip> zip(const Other& other, const Zip& z) const { return {*static_cast<T const*>(this), other, z}; } }; template <typename X> struct F_id : F<F_id<X>> { const X x; F_id(const X& x) : x(x) {} template <typename Callback> void operator()(Callback&& callback) const { callback(x); } }; template <typename Upstream, typename Other, typename Zip> struct F_zip : F<F_zip<Upstream, Other, Zip>> { Upstream upstream; Other other; Zip zip; F_zip(const Upstream& upstream, const Other& other, const Zip& zip) : upstream(upstream), other(other), zip(zip) {} template <typename Callback> void operator()(Callback&& callback) const { upstream([=](auto a) { other([=, &a](auto b) { callback(zip(a, b)); }); }); } }; void test_zip() { F_id(10) .zip(F_id(20), [](int x, int y) { return std::make_tuple(x, y); })([](atuo x) { auto [a, b] = x; printf("(%d, %d)\n", a, b); }); },c++,templates,functional-programming,C++,Templates,Functional Programming,我试图添加模板参数来声明a/b的类型,或者使用void*来存储a/b,但都失败了 有什么想法吗 ? a; ? b; bool is_a_present, is_b_present; template <typename Callback> void operator()(Callback&& callback) { upstream([=, &a, &b, &is_a_present, &is_b_present](auto x)

我试图添加模板参数来声明
a
/
b
的类型,或者使用
void*
来存储
a
/
b
,但都失败了

有什么想法吗

? a;
? b;
bool is_a_present, is_b_present;

template <typename Callback>
void operator()(Callback&& callback) {
  upstream([=, &a, &b, &is_a_present, &is_b_present](auto x) {
    a = x;
    is_a_present = true;
    if (is_b_present) callback(zip(a, b));
  });
  other([=, &a, &b, &is_a_present, &is_b_present](auto x)) {
    b = x;
    is_b_present = true;
    if (is_a_present) callback(zip(a, b));
  }
}