具有可变参数模板的功能组合

Functional composition with variadic templates

本文关键字:功能 组合 参数 变参      更新时间:2023-10-16

我的目标是获得使用以下确切语法的函数组合:

int main() {
    Function<std::string, int> f([](const std::string& s) {return s.length();});
    Function<int, double> g([](int x) {return x + 0.5;});
    Function<double, int> h([](double d) {return int(d+1);});
    std::cout << compose(g, f, "hello") << 'n';  // g(f("hello")) = 5.5
    std::cout << compose(h, g, f, "hello") << 'n';  // h(g(f("hello"))) = 6
}

通过稍微更改语法以使"hello"参数首先出现,我可以轻松地使用以下代码:

#include <iostream>
#include <functional>
#include <tuple>
#include <string>
template <typename D, typename R>
struct Function {
    using domain = const D&;
    using range = R;
    using function = std::function<range(domain)>;
    const function& f;
    Function (const function& f) : f(f) {}
    range operator()(domain x) const {return f(x);}
};
template <typename... Ts>
struct LastType {
    using Tuple = std::tuple<Ts...>;
    using type = typename std::tuple_element<std::tuple_size<Tuple>::value - 1, Tuple>::type;
};
template <typename F, typename G>
typename G::range compose (const typename F::domain& x, const G& g, const F& f) {
    return g(f(x));
}
template <typename F, typename... Rest>
auto compose (const typename LastType<Rest...>::type::domain& x, const F& f, const Rest&... rest) {
    return f(compose(x, rest...));
}
int main() {
    Function<std::string, int> f([](const std::string& s) {return s.length();});
    Function<int, double> g([](int x) {return x + 0.5;});
    Function<double, int> h([](double d) {return int(d+1);});
    std::cout << compose("hello", g, f) << 'n';  // g(f("hello")) = 5.5
    std::cout << compose("hello", h, g, f) << 'n';  // h(g(f("hello"))) = 6
}

这样做之后,我认为调整上面的代码以便获得我想要的确切语法(即"hello"位于列表末尾)是一项微不足道的任务,但它变得比我想象的要困难得多。 我尝试了以下操作,但无法编译:

#include <iostream>
#include <functional>
#include <tuple>
#include <string>
template <typename D, typename R>
struct Function {
    using domain = const D&;
    using range = R;
    using function = std::function<range(domain)>;
    const function& f;
    Function (const function& f) : f(f) {}
    range operator()(domain x) const {return f(x);}
};
template <typename F, typename G>
typename G::range compose (const G& g, const F& f, const typename F::domain& x) {
    return g(f(x));
}
template <typename F, typename... Rest>
auto compose (const F& f, const Rest&... rest) {
    return f(compose(rest...));
}
int main() {
    Function<std::string, int> f([](const std::string& s) {return s.length();});
    Function<int, double> g([](int x) {return x + 0.5;});
    Function<double, int> h([](double d) {return int(d+1);});
    std::cout << compose(g, f, "hello") << 'n';  // g(f("hello")) = 5.5
    std::cout << compose(h, g, f, "hello") << 'n';  // h(g(f("hello"))) = 6
}

而且我不知道如何解决它。有人可以帮我解决这个问题吗?

我想出的一个新想法是定义compose_,这将重新排序args...的参数(通过一些std::tuple操作),以便第一个元素排在最后,然后将该参数包传递给compose。 不过,这看起来非常混乱,即使它有效,也必须有一个更直接(和更短)的解决方案。

看起来以下内容也有效:

template <typename T>
const T& compose (const T& t) {
    return t;
}
template <typename F, typename... Rest>
typename F::range compose(const F& f, Rest... rest) {
    return f(compose(rest...));
}
这样

呢?

#include <iostream>    
#include <functional>
#include <tuple>
#include <string>
template <typename D, typename R>
struct Function {
    using domain = const D&;
    using range = R;
    using function = std::function<range(domain)>;
    const function& f;
    Function (const function& f) : f(f) {}
    range operator()(domain x) const {return f(x);}
};
template <typename F, typename X = typename F::domain>
typename F::range compose (const F& f, const X & x) {
    return f(x);
}
template <typename F, typename... Rest>
typename F::range  compose (const F& f, const Rest&... rest) {
    return f(compose(rest...));
}
int main() {
    Function<std::string, int> f([](const std::string& s) {return    s.length();});
    Function<int, double> g([](int x) {return x + 0.5;});
    Function<double, int> h([](double d) {return int(d+1);});
    std::cout << compose(g, f, "hello") << 'n';  // g(f("hello")) = 5.5
    std::cout << compose(h, g, f, "hello") << 'n';  // h(g(f("hello"))) = 6
}

您只能在 c++14 中将 auto 用于返回类型的compose()(如果我没记错的话)。

您的版本无法编译,因为您的可变参数compose()版本使用 N 个可变参数类型和 N 个参数

,而最终版本(非可变参数)使用 2 个类型和 3 个参数。换句话说,可变参数版本失去了最终的论点。您的版本无法编译,因为从不使用最终版本(非可变参数版本):编译器选择可变参数版本。添加typename X = typename F::domain(并用const X&更改const typename F::domain&)最终版本是首选,您的代码应该编译(至少使用 c++14)[由 Piotr Skotnicki 更正;谢谢]

PS:对不起,我的英语不好。