为什么下面的注释行没有编译?我可以使用std::transform和async吗?为了测试,它是硬编码的。最后,我想用它来获得数百万分。
#include <algorithm>
#include <future>
#include <iostream>
#include <vector>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
namespace bg = boost::geometry;
typedef bg::model::point<double, 2, bg::cs::cartesian> point_t;
typedef bg::model::polygon<point_t> polygon_t;
void CreatePolygon(polygon_t &poly)
{
polygon_t polygon2{ {{0.0, 0.0}, {0.0, 5.0}, {5.0, 5.0}, {5.0, 0.0}, {0.0, 0.0}},
{{1.0, 1.0}, {4.0, 1.0}, {4.0, 4.0}, {1.0, 4.0}, {1.0, 1.0}} };
poly = polygon2;
}
void PointsInPolygonMultiThreaded1()
{
polygon_t poly;
CreatePolygon(poly);
point_t p1(.5, .5), p2(2.0, 2.0), p3(3.0, 3.0), p4(4.5, 4.5);
std::vector<point_t> p_vec1({ p1, p2 });
std::vector<point_t> p_vec2({ p3, p4 });
std::vector<bool> vec_b1(p_vec1.size());
std::vector<bool>::iterator it;
//std::async(std::transform, begin(p_vec1), end(p_vec1), begin(vec_b1), [poly](point_t& x) {return boost::geometry::within(x, poly); });
std::vector<bool> vec_b2(p_vec1.size());
//std::async(std::transform, begin(p_vec2), end(p_vec2), begin(vec_b2), [poly](point_t& x) {return boost::geometry::within(x, poly); });
std::vector<bool> final_result(begin(vec_b1), end(vec_b1));
final_result.insert(end(final_result), begin(vec_b2), end(vec_b2));
}
由于std::transform
是一个模板函数,编译器在实例化函数时必须尝试推导模板参数,但这种推导是失败的。然后,编译继续尝试使用std::async
的其他重载,试图找到一个合适的替代方案,当它不能导致最终的编译错误时。
这可以通过向编译器提供足够的信息来计算出它无法推导出的模板参数来解决。一旦这样做的方式如下:
#include <algorithm>
#include <future>
#include <iostream>
#include <vector>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
namespace bg = boost::geometry;
typedef bg::model::point<double, 2, bg::cs::cartesian> point_t;
typedef bg::model::polygon<point_t> polygon_t;
void CreatePolygon(polygon_t &poly)
{
polygon_t polygon2{ {{0.0, 0.0}, {0.0, 5.0}, {5.0, 5.0}, {5.0, 0.0}, {0.0, 0.0}},
{{1.0, 1.0}, {4.0, 1.0}, {4.0, 4.0}, {1.0, 4.0}, {1.0, 1.0}} };
poly = polygon2;
}
void PointsInPolygonMultiThreaded1()
{
polygon_t poly;
CreatePolygon(poly);
point_t p1(.5, .5), p2(2.0, 2.0), p3(3.0, 3.0), p4(4.5, 4.5);
std::vector<point_t> p_vec1({ p1, p2 });
std::vector<point_t> p_vec2({ p3, p4 });
std::vector<bool> vec_b1(p_vec1.size());
std::vector<bool>::iterator it;
using InputItType = std::vector<point_t>::iterator;
using OutputItType = std::vector<bool>::iterator;
auto const unaryOp = [poly](point_t& x) {return boost::geometry::within(x, poly); };
using UnaryType = decltype(unaryOp);
// Provide the missing type information the compiler fails to deduce.
std::async(std::transform<InputItType, OutputItType, UnaryType>, begin(p_vec1), end(p_vec1), begin(vec_b1), unaryOp);
std::vector<bool> vec_b2(p_vec1.size());
std::async(std::transform<InputItType, OutputItType, UnaryType>, begin(p_vec2), end(p_vec2), begin(vec_b2), unaryOp);
std::vector<bool> final_result(begin(vec_b1), end(vec_b1));
final_result.insert(end(final_result), begin(vec_b2), end(vec_b2));
}