C++ concepts and boost geometry

60 Views Asked by At

I have an operator template, that calls a boost geometry function template for types T1, T2. If this function template cannot be instantiated for types T1, T2, then I want the compiler to choose a fallback operator template. How do I manage this using c++20 concepts?

The following does not compile:

#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);
}

Tried to compile with

g++ -std=c++20 -O2 -Wall -pedantic -pthread main.cpp && ./a.out

What am I doing wrong? Many thanks in advance!

0

There are 0 best solutions below