godotengine / godotengine/godot-cpp
[1.1] Crash at runtime if _init is missing; check for this at compile time
- Dominant language
- C++
- Stars
- 2.7k
- Forks
- 809
- Avg merge
- 1d 3h
- Merged PRs (30d)
- 8
Description
@karroffel and I discussed this on IRC:
> 17:51:59 < karroffel> Ah yes, now every script class *has* to have the _init function
> ...
> 17:56:13 < thomastc> maybe GODOT_CLASS could check for this at compile time using some template magick
I figured out the magick, and it's not even terrible. We could put the `static_assert`s into `register_class`:
```c++
#include
static_assert(std::is_same::value,
"Class has no _new() function; did you forget to add the GODOT_CLASS macro invocation?");
static_assert(std::is_same::value,
"GODOT_CLASS must have a void _init() function");
static_assert(std::is_default_constructible::value,
"GODOT_CLASS must be default-constructible");
static_assert(!std::has_virtual_destructor::value,
"GODOT_CLASS must not have a virtual destructor");
static_assert(std::is_destructible::value,
"GODOT_CLASS must not have a deleted destructor");
```
I'm _think_ all these requirements are necessary, but @karroffel would know better, that's why I'm not making a PR (yet?). Mainly I'm unsure whether virtual destructors (or any virtual functions) are indeed forbidden here, or whether they are safe despite all the unsafe casting that's going on.
Breaking any of these requirements except the `_init` one results in compile errors elsewhere, so the asserts are not strictly necessary; they just give nicer error messages.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.