Skip to content

Fix static inline template in defs.hpp#1962

Merged
dsnopek merged 1 commit intogodotengine:masterfrom
zhehangd:fix_static_inline
Mar 31, 2026
Merged

Fix static inline template in defs.hpp#1962
dsnopek merged 1 commit intogodotengine:masterfrom
zhehangd:fix_static_inline

Conversation

@zhehangd
Copy link
Copy Markdown
Contributor

I was using clang-18 + C++module to build my gdextension lib and encountered a compile error in binding a method normally. A MRE is given as follow:

// a.cppm
// A module that includes some godot-cpp header files.
// This is required to trigger the error.
module;

#include <godot_cpp/core/class_db.hpp>

export module a;
// main.cpp
// A source file that uses the module and uses `godot::nearest_power_of_2_templated`.
#include <godot_cpp/core/defs.hpp>
#include <print>

import a;

auto main() -> int {
  auto a = godot::nearest_power_of_2_templated<unsigned int>(12u);
  std::println("a = {}", a);
  return 0;
}

compile

INCDIR="<workspace>/include/"
LDDIR="<workspace>/lib/"
clang++ -std=c++23 -I"$INCDIR" a.cppm --precompile -o a.pcm
clang++ -std=c++23 -I"$INCDIR" -L"$LDDIR" main.cpp -fmodule-file=a=a.pcm a.pcm -lgodot-cpp.linux.template_release.x86_64 -o main

The error looks like this

main.cpp:7:12: error: no matching function for call to 'nearest_power_of_2_templated'
    7 |   auto a = godot::nearest_power_of_2_templated<unsigned int>(12u);
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

After some tests I realized that the fact nearest_power_of_2_templated uses static inline AND is a template is key of the issue.

template <typename T>
static _FORCE_INLINE_ T nearest_power_of_2_templated(T x)

I am not sure if this is a real error by C++ standard or just a clang bug for module, but it is true that (global function) static inline does not make too much sense in modern C++ and is equivalent to inline. In this particular case, I propose to just use constexpr instead, which is fully compatible and allows compile-time evaluation. This is the first motivation.

The second motivation is that the Engine codebase uses no static from the beginning, just inline. Meanwhile, the Engine has other functions (e.g. get_shift_from_power_of_2) marked with constexpr while godot-cpp does not. So I propose we use constexpr as a unified solution.

With this PR the code in godot-cpp becomes consistent with godot, except that godot has no constexpr in the two template functions is_power_of_2 nearest_power_of_2_templated. I intend to propose this change to godot as well.

Copy link
Copy Markdown
Member

@Ivorforce Ivorforce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All changes look like a strict improvement to me.

@Ivorforce Ivorforce added the enhancement This is an enhancement on the current functionality label Mar 31, 2026
Copy link
Copy Markdown
Collaborator

@dsnopek dsnopek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! These changes make sense to me and I can confirm that they mostly match what's in the engine

Assuming godotengine/godot#118027 doesn't take excessively long to be merged, I think we should off on merging here until that one is merged first

@dsnopek dsnopek added the waiting for Godot PRs that can't be merged until an engine PR is merged first label Mar 31, 2026
@dsnopek dsnopek added this to the 10.x milestone Mar 31, 2026
@dsnopek dsnopek added cherrypick:4.5 and removed waiting for Godot PRs that can't be merged until an engine PR is merged first labels Mar 31, 2026
@dsnopek dsnopek merged commit 319af8d into godotengine:master Mar 31, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cherrypick:4.5 enhancement This is an enhancement on the current functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants