有些算法实际上是可链接的,您可以在视图和/或动作命名空间中找到它们。
但您的代码表明您实际上有不同的问题。为什么没有允许结束管道链的签名算法?对于此类算法,我建议使用命名空间 reducer。这是一个工作代码示例:
#include <iostream>
#include <string>
#include <vector>
#include <range/v3/all.hpp>
using namespace std;
namespace view = ranges::view;
namespace action = ranges::action;
namespace reducer {
template <typename T>
class count {
T t;
public:
count(T t) : t(t) {}
template <typename Left>
T operator()(Left left) {
return ranges::count(left, t);
}
};
template <typename Left, typename T>
int operator|(Left left, count<T> right) {
return right(left);
}
}
int main (int argc, char * argv[])
{
const auto ints = std::vector<int>{1,2,1,3,1,4,1,5,1,6};
const auto num_ones = ints | reducer::count(1);
cout << num_ones << endl;
return 0;
}
Eric Niebler 说,人们像我们一样从臀部发出许多想法,但看不到深刻的后果。所以也许我们看不到这个想法有什么不好的地方。如果他通过您的问题并发表评论启发我们,那就太好了。
当然,他在 range-v3 中使用了 C++11,并且没有构造函数的类型推断,这个想法更难实现。