diff --git a/include/chaiscript/dispatchkit/proxy_constructors.hpp b/include/chaiscript/dispatchkit/proxy_constructors.hpp index 721d18ec..e2e9a4ba 100644 --- a/include/chaiscript/dispatchkit/proxy_constructors.hpp +++ b/include/chaiscript/dispatchkit/proxy_constructors.hpp @@ -22,12 +22,21 @@ namespace chaiscript template Proxy_Function build_constructor_(Class (*)(Params...)) { - auto call = [](auto && ... param){ - return Class(std::forward(param)...); - }; + if constexpr (!std::is_copy_constructible_v) { + auto call = [](auto && ... param) { + return std::make_shared(std::forward(param)...); + }; - return Proxy_Function( - chaiscript::make_shared>(call)); + return Proxy_Function( + chaiscript::make_shared (Params...), decltype(call)>>(call)); + } else if constexpr (true) { + auto call = [](auto && ... param){ + return Class(std::forward(param)...); + }; + + return Proxy_Function( + chaiscript::make_shared>(call)); + } } } } diff --git a/unittests/compiled_tests.cpp b/unittests/compiled_tests.cpp index 2388893c..f4c87509 100644 --- a/unittests/compiled_tests.cpp +++ b/unittests/compiled_tests.cpp @@ -1417,7 +1417,45 @@ TEST_CASE("Throw an exception when trying to add same conversion twice") std::cout << "My_int type conversion 2\n"; return my_int(x); })), chaiscript::exception::conversion_error); - } +TEST_CASE("Test if non copyable/movable types can be registered") +{ + struct Noncopyable { + Noncopyable() {str = "test";} + Noncopyable(const Noncopyable&) = delete; + Noncopyable& operator=(const Noncopyable&) = delete; + std::string str; + }; + + struct Nonmovable { + Nonmovable() {str = "test";} + Nonmovable(Nonmovable&&) = delete; + Nonmovable& operator=(Nonmovable&&) = delete; + + std::string str; + }; + + struct Nothing { + Nothing() {str = "test";} + + Nothing(Nothing&&) = delete; + Nothing& operator=(Nothing&&) = delete; + + Nothing(const Nothing&) = delete; + Nothing& operator=(const Nothing&) = delete; + + std::string str; + }; + + chaiscript::ChaiScript chai; + chai.add(chaiscript::user_type(), "Noncopyable"); + chai.add(chaiscript::constructor(), "Noncopyable"); + + chai.add(chaiscript::user_type(), "Nonmovable"); + chai.add(chaiscript::constructor(), "Nonmovable"); + + chai.add(chaiscript::user_type(), "Nothing"); + chai.add(chaiscript::constructor(), "Nothing"); +}