Skip to content

fix: Compile fix for MSVC 2022 in C++20 mode#778

Merged
JEnoch merged 7 commits intoeclipse-zenoh:mainfrom
jmachowinski:main
Apr 19, 2026
Merged

fix: Compile fix for MSVC 2022 in C++20 mode#778
JEnoch merged 7 commits intoeclipse-zenoh:mainfrom
jmachowinski:main

Conversation

@jmachowinski
Copy link
Copy Markdown
Contributor

@jmachowinski jmachowinski commented Apr 15, 2026

related to ros2/rmw_zenoh#968

This fixed compilation errors with MSCV 2022 in C++20 mode.
Note, the minimum c++ standard will be raised to 20 for the L-Release.

@jmachowinski jmachowinski force-pushed the main branch 2 times, most recently from a84273f to 3607e9c Compare April 15, 2026 13:07
@jmachowinski jmachowinski changed the title fix: Compile fix for MSVC 2022 fix: Compile fix for MSVC 2022 in C++20 mode Apr 15, 2026
Comment thread include/zenoh/detail/closures.hxx Outdated
template <class CC, class DD>
static void* into_context(CC&& call, DD&& drop) {
auto obj = new Closure<C, D, R, Args...>(std::forward<CC>(call), std::forward<DD>(drop));
auto obj = new Closure<std::remove_cv_t<std::remove_reference_t<CC>>,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change prevents creating closures from callable references (more precisely the closure will no longer use reference but force a copy, which might not even be available), which breaks current behavior.

Copy link
Copy Markdown
Contributor Author

@jmachowinski jmachowinski Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct this breaks the lvalue reference case.
If CC is a rvalue a copy / move is done anyway as _call should be a non reference type in this case.

I was thinking about a constexp if etc for the rvalue case, but I don't think it is worth it.
I reverted this, and went back to the initial try, which is to replace the lambdas by custom structs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an explanation why simple std::move(d) for D = std::remove_reference_t<decltype(d)) for lambda does not work ?

Comment thread include/zenoh/api/bytes.hxx Outdated
};
using DroppableType = typename detail::closures::Droppable<VectorDeleter>;
auto drop = DroppableType::into_context(std::move(VectorDeleter(ptr)));
auto drop = DroppableType::into_context(std::move(VectorDeleter({.ptr = ptr})));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not a valid C++17 code, aggregate initialization is only available, starting from C++20. Just into_context(std::move(VectorDeleter{ptr})) (or even just into_context(VectorDeleter{ptr})) should be good enough, the question stays though why std::move on lambda fails ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn, your are right this is also an c++20 feature...

I honestly don't know why the move fails on msvc. From my point of view it is valid, and gcc and clang are fine with it...
The compile error is also not really helpful on this one.. (see ros2/rmw_zenoh#968 (comment) for details).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I'm wondering will something simple like auto&& d = [p = ptr]() mutable { delete p; }; solve the problem ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just tried it, nope...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ros2 CI has just enforced c++20 as minimum, so now the compiler error is also visible there in english :
https://ci.ros2.org/job/ci_windows/27622/console

Any more ideas on what to try ?

@JEnoch
Copy link
Copy Markdown
Member

JEnoch commented Apr 19, 2026

@DenisBiryukov91 I'm merging this to unblock the ROS CI and progress for Lyrical release.
Please do a last review and fix or rollback if you think this PR might cause issues in other use cases.

@JEnoch JEnoch merged commit 481b71b into eclipse-zenoh:main Apr 19, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants