提问人:Acave At 提问时间:11/14/2023 更新时间:11/14/2023 访问量:43
C++ 概念和提升几何图形
C++ concepts and boost geometry
问:
我有一个运算符模板,它调用类型 的 boost 几何函数模板 , .如果无法为类型实例化此函数模板,则我希望编译器选择回退运算符模板。如何使用 c++20 概念进行管理?T1
T2
T1
T2
以下内容不编译:
#include <iostream>
#include <concepts>
#include <boost/geometry.hpp>
/*
* Concept requiring that the template function "boost::geometry::within<T1, T2>"
* can be instantiated for types T1, T2
*/
template<typename T1, typename T2>
concept WithinCanBeInstantiated = requires(const T1& t1, const T2& t2)
{
boost::geometry::within<T1, T2>(t1, t2);
};
struct Within {
/*
* Operator template that should be used, if "boost::geometry::within<T1, T2>"
* can be instantiated for types T1, T2
*/
template <typename T1, typename T2>
requires WithinCanBeInstantiated<T1, T2>
bool operator()(const T1& t1, const T2& t2) const
{
return boost::geometry::within<T1, T2>(t1, t2);
}
/*
* Operator template that should be used, if "boost::geometry::within<T1, T2>"
* cannot be instantiated for types T1, T2
*/
template <typename T1, typename T2>
requires (not WithinCanBeInstantiated<T1, T2>)
bool operator()(const T1& t1, const T2& t2) const
{
return false;
}
};
int main() {
typedef boost::geometry::model::d2::point_xy<double> point_type;
typedef boost::geometry::model::polygon<point_type> polygon_type;
polygon_type poly;
boost::geometry::read_wkt(
"POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)"
"(4.0 2.0, 4.2 1.4, 4.8 1.9, 4.4 2.2, 4.0 2.0))", poly);
point_type p(4, 1);
Within within;
std::cout << "within exists: " << within(p, poly) << " no within: " << within(poly, p);
}
尝试编译
g++ -std=c++20 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
我做错了什么? 提前非常感谢!
答: 暂无答案
评论
template <typename T> T min(T, T)
min(42, "bye")
42
"bye"
T
min