sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

Error in emplace method

Open paulocoutinhox opened this issue 1 year ago • 2 comments

Hi,

Im getting this error when build:

[100%] Building CXX object engine/extensions/scripting/lua-bindings/CMakeFiles/axlua.dir/Users/paulo/Developer/workspaces/cpp/axmol/3rdparty/yasio/yasio/bindings/lyasio.cpp.o
In file included from /Users/paulo/Developer/workspaces/cpp/axmol/3rdparty/yasio/yasio/bindings/lyasio.cpp:83:
In file included from /Users/paulo/Developer/workspaces/cpp/axmol/3rdparty/yasio/yasio/bindings/yasio_sol.hpp:29:
/Users/paulo/Developer/workspaces/cpp/axmol/3rdparty/lua/sol/sol.hpp:6755:10: error: no member named 'construct' in 'optional<type-parameter-0-0 &>'
 6755 |                         this->construct(std::forward<Args>(args)...);
      |                         ~~~~  ^
/Users/paulo/Developer/workspaces/cpp/axmol/3rdparty/lua/sol/sol.hpp:14541:32: warning: unknown warning group '-Wmaybe-uninitialized', ignored [-Wunknown-warning-option]
 14541 | #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
       |                                ^
/Users/paulo/Developer/workspaces/cpp/axmol/3rdparty/lua/sol/sol.hpp:17294:32: warning: unknown warning group '-Wmaybe-uninitialized', ignored [-Wunknown-warning-option]
 17294 | #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
       |                                ^
2 warnings and 1 error generated.

My solution:

/// Constructs the value in-place, destroying the current one if there is
/// one.
///
/// \group emplace
template <class... Args>
T& emplace(Args&&... args) noexcept {
    static_assert(std::is_constructible<T, Args&&...>::value, "T must be constructible with Args");

    *this = nullopt;
    new (static_cast<void*>(this)) optional(std::in_place, std::forward<Args>(args)...);
    return **this;
}

paulocoutinhox avatar May 25 '24 04:05 paulocoutinhox

I'm running into the same issue with GCC 15. Since there already is a fix ( https://github.com/ThePhD/sol2/commit/d805d027e0a0a7222e936926139f06e23828ce9f ), can a new release be created?

Maddimax avatar Sep 02 '24 07:09 Maddimax

Can you please create a minimal repro? While your fix is simple, it is however semantically different from the one in place.

deadlocklogic avatar Oct 06 '24 07:10 deadlocklogic