首页 > 解决方案 > 如何使用 C++20 模块包装实现?

问题描述

我在包装可比距离的增强几何实现时遇到了麻烦。我不确定是因为我使用的模块不正确,还是因为 boost 几何本身有问题。在模块文件中,我有:

module;

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>

export module module_interface;
export import "point.hpp"; // Defines MyPoint as a plain struct with members float x, y; 
export {
    BOOST_GEOMETRY_REGISTER_POINT_2D(MyPoint, float, cs::cartesian, x, y)
};

namespace m3 {
    export template <typename A>
        auto MyFunc(A& a, A& b) {
        return boost::geometry::comparable_distance(a, b);
    }

    export auto MyFunc2() {
        MyPoint a{ 0,0 }, b{ 1, 1 };
        return MyFunc(a, b);
    }
};

在调用程序中我有:

#include <iostream>
import module_interface;

using namespace m3;

int main() {

    MyPoint a{ 0,0 }, b{ 1, 1 };
        
    auto t = m3::MyFunc(a, b); // I want to do this, but it does not compile
    auto t1 = m3::MyFunc2(); // this does compiles and may help isolate problem
}

当我编译时,我得到一个冗长的错误(我觉得这没有帮助,但也许它对你有帮助)

> include\boost\geometry\strategies\cartesian\distance_pythagoras.hpp(235,1):
> error C3967: error importing 'type' from module 'module_interface'
> include\boost\geometry\strategies\comparable_distance_result.hpp(48):
> message : see reference to class template instantiation
> 'boost::geometry::strategy::distance::services::return_type<boost::geometry::strategy::distance::comparable::pythagoras<CalculationType>,MyPoint,MyPoint>'
> being compiled 1>        with 1>        [ 1>           
> CalculationType=void 1>        ]
> include\boost\geometry\strategies\comparable_distance_result.hpp(61):
> message : see reference to class template instantiation
> 'boost::geometry::resolve_strategy::comparable_distance_result<Geometry1,Geometry2,boost::geometry::strategy::distance::services::default_strategy<boost::geometry::point_tag,boost::geometry::point_tag,MyPoint,MyPoint,boost::geometry::cartesian_tag,boost::geometry::cartesian_tag,void>::type,true>'
> being compiled 1>        with 1>        [ 1>           
> Geometry1=MyPoint, 1>            Geometry2=MyPoint 1>        ]
> include\boost\geometry\strategies\comparable_distance_result.hpp(132):
> message : see reference to class template instantiation
> 'boost::geometry::resolve_strategy::comparable_distance_result<Geometry1,Geometry2,Strategy,true>'
> being compiled 1>        with 1>        [ 1>           
> Geometry1=MyPoint, 1>            Geometry2=MyPoint, 1>           
> Strategy=boost::geometry::default_strategy 1>        ]
include\boost\geometry\strategies\comparable_distance_result.hpp(243):
> message : see reference to class template instantiation
> 'boost::geometry::resolve_variant::comparable_distance_result<Geometry1,Geometry2,Strategy>'
> being compiled 1>        with 1>        [ 1>           
> Geometry1=MyPoint, 1>            Geometry2=MyPoint, 1>           
> Strategy=boost::geometry::default_strategy 1>        ]
include\boost\geometry\strategies\comparable_distance_result.hpp(248):
> message : see reference to class template instantiation
> 'boost::geometry::comparable_distance_result<Geometry1,Geometry2,boost::geometry::default_strategy>'
> being compiled 1>        with 1>        [ 1>           
> Geometry1=MyPoint, 1>            Geometry2=MyPoint 1>        ]
> include\boost\geometry\strategies\default_comparable_distance_result.hpp(37):
> message : see reference to class template instantiation
> 'boost::geometry::comparable_distance_result<Geometry1,Geometry2,void>'
> being compiled 1>        with 1>        [ 1>           
> Geometry1=MyPoint, 1>            Geometry2=MyPoint 1>        ]
> 1>test_modules\module_interface\module_interface.ixx(15):
> message : see reference to class template instantiation
> 'boost::geometry::default_comparable_distance_result<A,A>' being
> compiled 1>        with 1>        [ 1>            A=MyPoint 1>       
> ]
> 1>include\boost\geometry\algorithms\detail\comparable_distance\interface.hpp(358):
> message : see reference to function template instantiation
> 'default_comparable_distance_result<Geometry1,Geometry2>::type
> boost::geometry::comparable_distance(const Geometry1 &,const Geometry2
> &)' being compiled
> 1>test_modules\module_consumer\consumer.cpp(10):
> message : see reference to function template instantiation 'auto
> m3::MyFunc<MyPoint>(A &,A &)' being compiled 1>        with 1>       
> [ 1>            A=MyPoint 1>        ]

标签: c++c++20boost-geometryc++-modules

解决方案


推荐阅读