M3-3420 avatar image
0 Votes"
M3-3420 asked MinxinYu-MSFT edited

problem calling template function defined in module

I was unable to make a minimal working example. I hope this suffices. If not, please let me know what other information is helpful.

In a module called unit_spatial.ixx I have the following snippet:

 template <typename RTree, typename Geometry>
 inline auto k_within(RTree const &map, Geometry const &geometry, float radius, unsigned k = std::numeric_limits<int>::max()) {
     auto nearest = std::ranges::subrange(map.qbegin(bgi::nearest(geometry, k)), map.qend()); // compiler doesn't complain about this
     auto f       = std::ranges::views::filter( is_within(geometry, radius));   // compiler ok with this
     auto g       = std::ranges::views::take_while(is_within(geometry, radius)); // compiler ok with this too
     //return nearest | f;  // client would compile if I used f
     return nearest | g;  // fails to compile a client calling this function: fatal error C1116: unrecoverable error importing module 'unit_spatial'

In the client I have imported unit_spatial and have the following snippet

 // ....
         Point2D origin(0, 0);
         UnitMap map(us.begin(), us.end());                  // create packed map
         auto    rng = k_within(map, origin, 5.0f, 100);  // (1) search 100 units for those within radius <= 5; Fails as described above

I'm using boost geometry for my RTree and MS STL library on VS 2022 preview 7. Apart from noticing the different signatures of std::ranges::views::filter and std::ranges::views::take_while, I can't figure out how this could be.
Also, If I call the std::ranges::views::take_while(is_within(geometry, radius)) from the client that works too. It only fails when I call into a module that tries the same thing.
The compiler error is uninformative to me:

on line (1). fatal error C1116: unrecoverable error importing module 'unit_spatial'

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Can other projects with modules work well ? Because I do not see any code of export in your sample.
Please check if you mix import and #include in your code?
Here is a similar thread I found.

0 Votes 0 ·

Thanks. My module is exported and the client that imports it compiles fine when I use std::ranges::views::filter instead of std::ranges::views::take_while (it won't function fine until I can get take_while to work).
I wouldn't be surprised if the problem is similar to the thread you pointed to because I do have mixed headers (some include and some import). AFAICT there will never be a way out of that in c++.
Sorry, but isolating the problem with a simple example has been hard. I'm hoping someone has had similar trouble with take_while and has a work around.
If not, I understand that I haven't given much to go on.

0 Votes 0 ·

Please make sure to put the import directive below the include.
I am afraid we need a a simple example to analyze the cause since the snippet requires other libraries to run.

0 Votes 0 ·

0 Answers