From abad04b1af55fb2a45e3ab672f361bc72626f30b Mon Sep 17 00:00:00 2001 From: Bart Janssens Date: Tue, 26 Sep 2023 18:06:02 +0200 Subject: [PATCH] Don't store constants as references Closes issue https://github.com/JuliaInterop/CxxWrap.jl/issues/374 --- examples/types.cpp | 4 ++++ include/jlcxx/module.hpp | 5 +++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/examples/types.cpp b/examples/types.cpp index 89983d8..68c3b6f 100644 --- a/examples/types.cpp +++ b/examples/types.cpp @@ -367,6 +367,10 @@ JLCXX_MODULE define_julia_module(jlcxx::Module& types) types.set_const("EnumClassBlue", EnumClass::blue); types.method("check_red", [] (const EnumClass c) { return c == EnumClass::red; }); + static const EnumClass stored_blue = EnumClass::blue; + types.method("check_enum_byref", [] (const EnumClass& c) { return c == EnumClass::red; }); + types.set_const("StoredBlue", stored_blue); + types.add_type("Foo") .constructor>() .method("name", [](Foo& f) { return f.name; }) diff --git a/include/jlcxx/module.hpp b/include/jlcxx/module.hpp index 933d994..ef83521 100644 --- a/include/jlcxx/module.hpp +++ b/include/jlcxx/module.hpp @@ -629,12 +629,13 @@ class JLCXX_API Module template void set_const(const std::string& name, T&& value) { - static_assert(IsMirroredType::type>::value, "set_const can only be applied to IsMirrored types (e.g. numbers or standard layout structs)"); + using plain_type = remove_const_ref; + static_assert(IsMirroredType::value, "set_const can only be applied to IsMirrored types (e.g. numbers or standard layout structs)"); if(get_constant(name) != nullptr) { throw std::runtime_error("Duplicate registration of constant " + name); } - set_constant(name, box(value)); + set_constant(name, box(value)); } std::string name() const