mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2026-05-01 03:19:28 +08:00
With gcc 13.3.0 an `-Wnoexcept` I warnings like the following:
```
In file included from .../include/c++/13.3.0/bits/char_traits.h:57,
from .../include/c++/13.3.0/string_view:40,
from .../ChaiScript/include/chaiscript/chaiscript_defines.hpp:23,
from .../ChaiScript/include/chaiscript/chaiscript_basic.hpp:10,
from .../ChaiScript/src/test_module.cpp:2:
.../include/c++/13.3.0/bits/stl_construct.h: In instantiation of ‘constexpr decltype (::new(void*(0)) _Tp) std::construct_at(_Tp*, _Args&& ...) [with _Tp = Type2; _Args = {Type2}; decltype (::new(void*(0)) _Tp) = Type2*]’:
.../include/c++/13.3.0/bits/stl_construct.h:115:21: required from ‘constexpr void std::_Construct(_Tp*, _Args&& ...) [with _Tp = Type2; _Args = {Type2}]’
.../include/c++/13.3.0/bits/alloc_traits.h:661:19: required from ‘static constexpr void std::allocator_traits<std::allocator<void> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = Type2; _Args = {Type2}; allocator_type = std::allocator<void>]’
.../include/c++/13.3.0/bits/shared_ptr_base.h:604:39: required from ‘std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {Type2}; _Tp = Type2; _Alloc = std::allocator<void>; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’
.../include/c++/13.3.0/bits/shared_ptr_base.h:971:16: required from ‘std::__shared_count<_Lp>::__shared_count(_Tp*&, std::_Sp_alloc_shared_tag<_Alloc>, _Args&& ...) [with _Tp = Type2; _Alloc = std::allocator<void>; _Args = {Type2}; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’
.../include/c++/13.3.0/bits/shared_ptr_base.h:1712:14: required from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::allocator<void>; _Args = {Type2}; _Tp = Type2; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’
.../include/c++/13.3.0/bits/shared_ptr.h:464:59: required from ‘std::shared_ptr<_Tp>::shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::allocator<void>; _Args = {Type2}; _Tp = Type2]’
.../include/c++/13.3.0/bits/shared_ptr.h:1009:14: required from ‘std::shared_ptr<std::_NonArray<_Tp> > std::make_shared(_Args&& ...) [with _Tp = Type2; _Args = {Type2}; _NonArray<_Tp> = Type2]’
.../ChaiScript/include/chaiscript/dispatchkit/boxed_value.hpp:121:37: required from ‘static auto chaiscript::Boxed_Value::Object_Data::get(T, bool) [with T = Type2]’
.../ChaiScript/include/chaiscript/dispatchkit/boxed_value.hpp:133:34: required from ‘chaiscript::Boxed_Value::Boxed_Value(T&&, bool) [with T = Type2; <template-parameter-1-2> = void]’
.../ChaiScript/include/chaiscript/dispatchkit/type_conversions.hpp:480:26: required from ‘chaiscript::Type_Conversion chaiscript::type_conversion(const Callable&) [with From = TestBaseType; To = Type2; Callable = create_chaiscript_module_test_module()::<lambda(const TestBaseType&)>; Type_Conversion = std::shared_ptr<detail::Type_Conversion_Base>]’
.../ChaiScript/src/test_module.cpp:194:58: required from here
.../include/c++/13.3.0/bits/stl_construct.h:95:14: warning: noexcept-expression evaluates to ‘false’ because of a call to ‘Type2::Type2(Type2&&)’ [-Wnoexcept]
95 | noexcept(noexcept(::new((void*)0) _Tp(std::declval<_Args>()...)))
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.../ChaiScript/src/test_module.cpp:60:7: note: but ‘Type2::Type2(Type2&&)’ does not throw; perhaps it should be declared ‘noexcept’
60 | class Type2 {
| ^~~~~
```
Fix this by introducing `noexcept` like proposed in the warning.
214 lines
6.3 KiB
C++
214 lines
6.3 KiB
C++
|
|
#include <chaiscript/chaiscript_basic.hpp>
|
|
#include <chaiscript/dispatchkit/bootstrap.hpp>
|
|
#include <string>
|
|
|
|
class TestBaseType {
|
|
public:
|
|
TestBaseType() noexcept
|
|
: val(10)
|
|
, const_val(15)
|
|
, mdarray{} {
|
|
}
|
|
TestBaseType(int) noexcept
|
|
: val(10)
|
|
, const_val(15)
|
|
, mdarray{} {
|
|
}
|
|
TestBaseType(int *) noexcept
|
|
: val(10)
|
|
, const_val(15)
|
|
, mdarray{} {
|
|
}
|
|
|
|
TestBaseType(const TestBaseType &other) noexcept
|
|
: val(other.val)
|
|
, const_val(other.const_val)
|
|
, const_val_ptr(&const_val)
|
|
, func_member(other.func_member) {
|
|
}
|
|
|
|
TestBaseType(TestBaseType &&other) noexcept
|
|
: val(other.val)
|
|
, const_val(other.const_val)
|
|
, const_val_ptr(&const_val)
|
|
, func_member(std::move(other.func_member)) {
|
|
}
|
|
|
|
TestBaseType &operator=(TestBaseType &&) = delete;
|
|
TestBaseType &operator=(const TestBaseType &) = delete;
|
|
|
|
virtual ~TestBaseType() = default;
|
|
virtual int func() { return 0; }
|
|
|
|
int base_only_func() { return -9; }
|
|
|
|
const TestBaseType &constMe() const { return *this; }
|
|
|
|
int val;
|
|
const int const_val;
|
|
const int *const_val_ptr = &const_val;
|
|
|
|
const int *get_const_val_ptr() { return const_val_ptr; }
|
|
|
|
int mdarray[2][3][5];
|
|
std::function<int(int)> func_member;
|
|
|
|
void set_string_val(std::string &t_str) { t_str = "42"; }
|
|
};
|
|
|
|
class Type2 {
|
|
public:
|
|
Type2(TestBaseType t_bt) noexcept
|
|
: m_bt(std::move(t_bt))
|
|
, m_str("Hello World") {
|
|
}
|
|
|
|
int get_val() const { return m_bt.val; }
|
|
|
|
const char *get_str() const { return m_str.c_str(); }
|
|
|
|
private:
|
|
TestBaseType m_bt;
|
|
std::string m_str;
|
|
};
|
|
|
|
enum TestEnum {
|
|
TestValue1 = 1
|
|
};
|
|
|
|
int to_int(TestEnum t) {
|
|
return t;
|
|
}
|
|
|
|
class TestDerivedType : public TestBaseType {
|
|
public:
|
|
int func() override { return 1; }
|
|
int derived_only_func() { return 19; }
|
|
};
|
|
|
|
class TestMoreDerivedType : public TestDerivedType {
|
|
public:
|
|
};
|
|
|
|
std::shared_ptr<TestBaseType> derived_type_factory() {
|
|
return std::make_shared<TestDerivedType>();
|
|
}
|
|
|
|
std::shared_ptr<TestBaseType> more_derived_type_factory() {
|
|
return std::make_shared<TestMoreDerivedType>();
|
|
}
|
|
|
|
std::shared_ptr<TestBaseType> null_factory() {
|
|
return std::shared_ptr<TestBaseType>();
|
|
}
|
|
|
|
void update_shared_ptr(std::shared_ptr<TestBaseType> &ptr) {
|
|
ptr = std::make_shared<TestDerivedType>();
|
|
}
|
|
|
|
void nullify_shared_ptr(std::shared_ptr<TestBaseType> &ptr) {
|
|
ptr = nullptr;
|
|
}
|
|
|
|
std::string hello_world() {
|
|
return "Hello World";
|
|
}
|
|
|
|
static int global_i = 1;
|
|
|
|
int *get_new_int() {
|
|
return &global_i;
|
|
}
|
|
|
|
// MSVC doesn't like that we are using C++ return types from our C declared module
|
|
// but this is the best way to do it for cross platform compatibility
|
|
#ifdef CHAISCRIPT_MSVC
|
|
#pragma warning(push)
|
|
#pragma warning(disable : 4190)
|
|
#endif
|
|
|
|
#ifdef __llvm__
|
|
#pragma clang diagnostic push
|
|
#pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
|
|
#endif
|
|
|
|
CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_test_module() {
|
|
chaiscript::ModulePtr m(new chaiscript::Module());
|
|
|
|
m->add(chaiscript::fun(hello_world), "hello_world");
|
|
|
|
m->add(chaiscript::user_type<TestBaseType>(), "TestBaseType");
|
|
m->add(chaiscript::user_type<TestDerivedType>(), "TestDerivedType");
|
|
m->add(chaiscript::user_type<TestMoreDerivedType>(), "TestMoreDerivedType");
|
|
m->add(chaiscript::user_type<Type2>(), "Type2");
|
|
|
|
m->add(chaiscript::constructor<TestBaseType()>(), "TestBaseType");
|
|
// m->add(chaiscript::constructor<TestBaseType (int)>(), "TestBaseType");
|
|
m->add(chaiscript::constructor<TestBaseType(const TestBaseType &)>(), "TestBaseType");
|
|
m->add(chaiscript::constructor<TestBaseType(int *)>(), "TestBaseType");
|
|
|
|
m->add(chaiscript::constructor<TestDerivedType()>(), "TestDerivedType");
|
|
m->add(chaiscript::constructor<TestDerivedType(const TestDerivedType &)>(), "TestDerivedType");
|
|
|
|
m->add(chaiscript::constructor<TestMoreDerivedType()>(), "TestMoreDerivedType");
|
|
m->add(chaiscript::constructor<TestMoreDerivedType(const TestMoreDerivedType &)>(), "TestMoreDerivedType");
|
|
|
|
/// \todo automatic chaining of base classes?
|
|
m->add(chaiscript::base_class<TestBaseType, TestDerivedType>());
|
|
m->add(chaiscript::base_class<TestBaseType, TestMoreDerivedType>());
|
|
m->add(chaiscript::base_class<TestDerivedType, TestMoreDerivedType>());
|
|
|
|
m->add(chaiscript::fun(&TestDerivedType::derived_only_func), "derived_only_func");
|
|
|
|
m->add(chaiscript::fun(&derived_type_factory), "derived_type_factory");
|
|
m->add(chaiscript::fun(&more_derived_type_factory), "more_derived_type_factory");
|
|
m->add(chaiscript::fun(&null_factory), "null_factory");
|
|
|
|
m->add(chaiscript::fun(&TestDerivedType::func), "func");
|
|
|
|
m->add(chaiscript::fun(&TestBaseType::func), "func");
|
|
m->add(chaiscript::fun(&TestBaseType::val), "val");
|
|
m->add(chaiscript::fun(&TestBaseType::const_val), "const_val");
|
|
m->add(chaiscript::fun(&TestBaseType::const_val_ptr), "const_val_ptr");
|
|
m->add(chaiscript::fun(&TestBaseType::get_const_val_ptr), "get_const_val_ptr");
|
|
m->add(chaiscript::fun(&TestBaseType::base_only_func), "base_only_func");
|
|
m->add(chaiscript::fun(&TestBaseType::set_string_val), "set_string_val");
|
|
|
|
m->add(chaiscript::fun(&TestBaseType::mdarray), "mdarray");
|
|
chaiscript::bootstrap::array<int[2][3][5]>("IntArray_2_3_5", *m);
|
|
chaiscript::bootstrap::array<int[3][5]>("IntArray_3_5", *m);
|
|
chaiscript::bootstrap::array<int[5]>("IntArray_5", *m);
|
|
|
|
// member that is a function
|
|
m->add(chaiscript::fun(&TestBaseType::func_member), "func_member");
|
|
m->add(chaiscript::fun(&get_new_int), "get_new_int");
|
|
|
|
m->add_global_const(chaiscript::const_var(TestValue1), "TestValue1");
|
|
|
|
m->add(chaiscript::user_type<TestEnum>(), "TestEnum");
|
|
|
|
m->add(chaiscript::fun(&to_int), "to_int");
|
|
m->add(chaiscript::fun(&TestBaseType::constMe), "constMe");
|
|
|
|
m->add(chaiscript::type_conversion<TestBaseType, Type2>([](const TestBaseType &t_bt) { return Type2(t_bt); }));
|
|
|
|
m->add(chaiscript::fun(&Type2::get_val), "get_val");
|
|
m->add(chaiscript::fun(&Type2::get_str), "get_str");
|
|
m->add(chaiscript::type_conversion<const char *, std::string>());
|
|
m->add(chaiscript::constructor<Type2(const TestBaseType &)>(), "Type2");
|
|
|
|
m->add(chaiscript::fun(&update_shared_ptr), "update_shared_ptr");
|
|
m->add(chaiscript::fun(&nullify_shared_ptr), "nullify_shared_ptr");
|
|
|
|
return m;
|
|
}
|
|
|
|
#ifdef __llvm__
|
|
#pragma clang diagnostic pop
|
|
#endif
|
|
|
|
#ifdef CHAISCRIPT_MSVC
|
|
#pragma warning(pop)
|
|
#endif
|