如何解决C++ 中的通用多功能函数组合/流水线
是否可以在 C++ 20 中实现通用的多函子组合/流水线?
struct F{//1st multi-functor
template<typename T> void operator()(const T& t){/*...*/}
};
struct G{//2nd multi-functor
template<typename T> void operator()(const T& t){/*...*/}
};
F f;
G g;
auto pipe = f | g;//what magic should happen here to achieve g(f(...)) ? how exactly to overload the operator|()?
pipe(123); //=> g(f(123);
pipe("text");//=> g(f("text");
编辑: 我尝试了这两个建议(来自@Some_programmer_dude 和@Jarod42),但我在错误中迷失了:
- 建议像@Some_programmer_dude 一样重载运算符|()
template<class Inp,class Out>
auto operator|(Inp inp,Out out){
return [inp,out](const Inp& arg){
out(inp(arg));
};
}
生成:
2>main.cpp(71,13): error C3848: expression having type 'const Inp' would lose some const-volatile qualifiers in order to call 'void F::operator ()<F>(const T &)'
2> with
2> [
2> Inp=F
2> ]
2> and
2> [
2> T=F
2> ]
- 直接使用 lambda 而不是像 @Jarod42 建议的那样重载 operator|():
auto pipe = [=](const auto& arg){g(f(arg));};
生成:
2>main.cpp(86,52): error C3848: expression having type 'const F' would lose some const-volatile qualifiers in order to call 'void F::operator ()<_T1>(const T &)'
2> with
2> [
2> _T1=int,2> T=int
2> ]
解决方法
你快到了
template<class Inp,class Out>
auto operator|(Inp inp,Out out){
return [inp,out](const Inp& arg){
out(inp(arg));
};
}
struct F{//1st multi-functor
template<typename T> void operator()(const T& t){/*...*/}
};
struct G{//2nd multi-functor
template<typename T> void operator()(const T& t){/*...*/}
};
F f;
G g;
auto pipe = f | g;
但是,您忽略了一些细微差别。
首先,lambda 中的 out
和 inp
是 const
,因为闭包类型有一个 const
限定的 operator()
。另外,lambda 的参数类型应该是 auto
,而不是 Inp
,否则你只能pipe(f)
。
您想为某个 g(f(x))
调用 x
,但 f
的返回类型是 void
,这需要其他内容。
您提供的 |
与算术 |
会产生歧义。
struct some_type {};
template<typename F>
concept unary_invocable = std::invocable<F,some_type>;
template <unary_invocable Inp,unary_invocable Out>
auto operator|(Inp inp,out](auto&& arg){
return out(inp(std::forward<decltype(arg)>(arg)));
};
}
struct F{//1st multi-functor
template<typename T> auto operator()(const T& t) const {/*... return something*/}
};
struct G{//2nd multi-functor
template<typename T> auto operator()(const T& t) const {/*... return something*/}
};
,
这里有一个快速的小图书馆。
#define RETURNS(...) \
noexcept(noexcept(__VA_ARGS__)) \
-> decltype(__VA_ARGS__) \
{ return __VA_ARGS__; }
namespace ops {
template<class D>
struct op_tag;
template<class Second,class First>
struct pipe_t;
template<class D>
struct op_tag {
D const& self() const { return *static_cast<D const*>(this); }
D& self() { return *static_cast<D*>(this); }
auto operator()(auto&&...args) const
RETURNS( self()(decltype(args)(args)...) )
auto operator()(auto&&...args)
RETURNS( self()(decltype(args)(args)...) )
};
template<class Second,class First>
struct pipe_t:op_tag<pipe_t<Second,First>> {
Second second;
First first;
pipe_t( Second second_,First first_ ):
second(std::move(second_)),first(std::move(first_))
{}
auto operator()(auto&&...args)
RETURNS( second(first(decltype(args)(args)...)) )
auto operator()(auto&&...args) const
RETURNS( second(first(decltype(args)(args)...)) )
};
template<class Second,class First>
auto operator|(op_tag<First> const& first,op_tag<Second> const& second)
RETURNS( pipe_t<Second,First>{ second.self(),first.self() } )
}
以贪婪的方式重载运算符被认为是粗鲁的。您只希望您的运算符重载参与您特别支持的类型。
这里我要求类型继承自 op_tag<T>
以表明它们有兴趣成为一个操作。
然后我们稍微修改您的代码:
struct F:ops::op_tag<F>{//1st multi-functor
template<typename T>
auto operator()(const T& t){
std::cout << "f(" << t << ")";
return -1;
}
};
struct G:ops::op_tag<G>{//2nd multi-functor
template<typename T> auto operator()(const T& t){
std::cout << "g(" << t << ")";
return 7;
}
};
添加标签和返回值(否则,f(g(x)) 没有意义,除非 g 返回一些东西)。
你写的代码现在可以工作了。
如果您愿意,我们还可以添加对 std::function
甚至原始函数的支持。您可以在 operator|
中添加合适的 namespace ops
重载,并要求人们 using ops::operator|
将运算符带入范围(或将其与 op_tag
类型一起使用)。
输出:
f(123)g(-1)f(text)g(-1)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。