我正在尝试在使用模板化内核的函数中使用 CGAL 的高效 Ransac 算法,这里是要重现的最小代码。
#include <CGAL/property_map.h>
#include <CGAL/Point_with_normal_3.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Shape_detection/Efficient_RANSAC.h>
// Type declarations.
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
template < typename K > //comment for working version
void funcTest() {
//typedef CGAL::Exact_predicates_inexact_constructions_kernel K; //uncomment for working version
typedef std::tuple<typename K::Point_3,typename K::Vector_3, size_t, bool> Point_and_normals;
typedef CGAL::Nth_of_tuple_property_map<0, Point_and_normals> Point_map;
typedef CGAL::Nth_of_tuple_property_map<1, Point_and_normals> Normal_map;
typedef CGAL::Shape_detection::Efficient_RANSAC_traits
<K, std::vector<Point_and_normals>, Point_map, Normal_map> TraitsShape;
typedef CGAL::Shape_detection::Efficient_RANSAC<TraitsShape> Efficient_ransac;
typedef CGAL::Shape_detection::Plane<TraitsShape> PlaneRansac;
std::vector<Point_and_normals> points;
Efficient_ransac ransac;
ransac.set_input(points);
ransac.add_shape_factory<PlaneRansac>();
ransac.detect();
}
int main (int argc, char** argv) {
funcTest<Kernel>(); //comment for working version
//funcTest()); //uncomment for working version
return 0;
}
在此代码中,模板化版本不会构建并出现此错误
tester.cpp:24:39: error: expected primary-expression before ‘>’ token
24 | ransac.add_shape_factory<PlaneRansac>();
| ^
tester.cpp:24:41: error: expected primary-expression before ‘)’ token
24 | ransac.add_shape_factory<PlaneRansac>();
但是,显式内核不存在该问题,我猜测它可能来自类型名问题,但我不确定我在这方面可能做错了什么。 有任何意见、建议欢迎留言
最佳答案
问题是编译器不知道标记 <
是否紧随add_shape_factory
之后是小于运算符或模板参数列表的开头。
我们可以使用 .template
调用成员函数模板时构造来解决这个问题,如下所示:
ransac.template add_shape_factory<PlaneRansac>();
//-----^^^^^^^^------------------------------------->added template keyword here to indicate that it is a member function template
.template
用于告诉编译器 <
token 是模板参数列表的开头,并且不小于运算符。
关于c++ - Efficient Ransac 的模板化内核存在问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72269958/