我的目标是让功能组合使用完全语法:
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
}
完成后,我认为调整上面的代码是一项微不足道的任务,这样我就可以获得我想要的确切语法(即使用&#34; hello&#34;位于列表的末尾),但是它变得比我想象的更难。我尝试了以下,但没有编译:
#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
。这看起来非常混乱,即使它有效,也必须有一个更直接(和更短)的解决方案。
答案 0 :(得分:2)
看起来以下内容也有效:
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...));
}
答案 1 :(得分:1)
#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个参数,而最终版本(不是varidic)使用2种类型和3个参数。换句话说,可变参数版本丢失了最终参数。typename X = typename F::domain
(并使用const typename F::domain&
更改const X&
)最终版本是首选,您的代码应该编译(至少使用c ++ 14)[由Piotr Skotnicki修正;感谢]
p.s:抱歉我的英语不好。