我已经编写了自己的 compose 方法,该方法适用于 POD,如何优化它以处理复制成本高昂的用户定义类型?
#include <iostream>
template<typename T, typename U, typename V>
std::function<V(T&&)> compose(std::function<V(U)> f, std::function<U(T)> g)
{
std::function<V(T&&)> result = [&](T&& value){
return f(g(value));
};
return result;
}
int main(int argc, const char * argv[])
{
std::function<int(int)> f = [](int v){ return v + 1;};
std::function<int(int)> g = [](int v){ return v + 2;};
std::function<int(int)> h = compose(f, g);
std::cout << "compose(f,g): " << (h(5)) << std::endl;
return 0;
}
布莱尔