我的目标是拥有一个模板函数,它将输入对象的std :: vector和函数对象作为输入。然后,此模板函数将使用函数对象和线程池将输入向量转换为转换对象的std :: vector。
下面包含示例代码。
我真的希望能够使用比首先创建本地函数对象更短的语法,然后传递所有模板参数。
使用gcc编译:g ++ -std = C ++ 0x bla.cpp
#include <vector>
#include <functional>
#include <iostream>
// SYNTAX:
// vector<ResultType> transformed = multiTransform(const vector<InputType>, Transform t)
// where Transform t takes a single InputType as an argument
// ConvertedType has to be default constructible
template <class ConvertedType, class InputType, class Transform>
std::vector<ConvertedType> multiTransform(const std::vector<InputType>& inputs, Transform t) {
std::vector<ConvertedType> results(inputs.size());
{
// boost::threadpool::pool pool(boost::thread::hardware_concurrency());
for(auto it = inputs.begin(); it != inputs.end(); ++it){
auto inputDereferenced = *it;
auto functor = [&, it, inputDereferenced](){
auto result = t(inputDereferenced);
results[it - inputs.begin()] = std::move(result);
};
// pool.schedule(functor);
functor();
}
}
return results;
}
int main() {
std::vector<int> input = {1,2,3};
// auto output = multiTransform(input, [](int a){return float(a);}); // does not compile
auto lambda = [](int a){return a/2.0;};
auto output = multiTransform<float, int, decltype(lambda)>(input, lambda);
for(auto it : output){
std::cout << it << std::endl;
}
}
答案 0 :(得分:3)
这适用于g ++ 4.6.3:
auto output = multiTransform<float>(input, [](int a){return a/2.0;});
您还可以使模板声明更复杂:
template <class InputType, class Transform>
auto
multiTransform(const std::vector<InputType>& inputs, Transform t)
-> std::vector<decltype(t(*inputs.begin())) >
{
typedef decltype(t(*inputs.begin())) ConvertedType;
std::vector<ConvertedType> results(inputs.size());
{
// boost::threadpool::pool pool(boost::thread::hardware_concurrency());
for(auto it = inputs.begin(); it != inputs.end(); ++it){
auto inputDereferenced = *it;
auto functor = [&, it, inputDereferenced](){
auto result = t(inputDereferenced);
results[it - inputs.begin()] = std::move(result);
};
// pool.schedule(functor);
functor();
}
}
return results;
}
然后你可以使用
auto output = multiTransform(input, [](int a){return a/2.0;});