zserio icon indicating copy to clipboard operation
zserio copied to clipboard

InplaceOptionalHolder isn't nothrow_move_constructible

Open MikeLankamp-TomTom opened this issue 11 months ago • 1 comments

Zserio version and language Zserio: 2.16.0 Language: C++

Describe the bug static_assert(std::is_nothrow_move_constructible<zserio::InplaceOptionalHolder<int>>) fails. This is unexpected considering int is nothrow_move_constructible and causes generated objects that use it to also fail that check. This causes pessimization because a std::move() of any type using an InplaceOptionalHolder anywhere in its descendants has to do a copy instead.

This problems is caused because:

  1. in_place_storage seems to be no-copy/no-move by design, since inplace_optional_holder access the internal storage directly in its own copy constructor (via getStorage() or getObject()), and
  2. inplace_optional_holder(inplace_optional_holder&&) doesn't access the internal storage, but attempts to move the in_place_storage object itself, which cannot happen because the point above.

So I'd say, either:

  • make in_place_storage copyable/movable (and update inplace_optional_holder to not use getStorage()/getObject() so much), or
  • update all inplace_optional_holder constructors to use getStorage()/getObject() like the other constructors.

How to reproduce Compile the following file:

#include <zserio/OptionalHolder.h>

static_assert(std::is_nothrow_move_constructible<zserio::InplaceOptionalHolder<int>>)

Expected behavior The compilation passes because the static assert isn't triggered.

Additional context N/A

MikeLankamp-TomTom avatar Jan 31 '25 09:01 MikeLankamp-TomTom