C++ usage guidelines
解释
C++17. While modern C++ brings a lot of opportunities to write faster, more readable code, we chose to restrict our usage of C++ to a subset for a few reasons:
- It makes it easier to review code in online editors. This is because engine contributors don’t always have access to a full-featured IDE while reviewing code.
- It makes the code easier to grasp for beginner contributors (who may not be professional C++ programmers). Godot’s codebase is known to be easy to learn from, and we’d like to keep it that way.
To get your pull request merged, it needs to follow the C++ usage guidelines outlined here. Of course, you can use features not allowed here in your own C++ modules or GDNative scripts.
注解
Prior to Godot 4.0, the C++ standard used throughout the codebase was C++03, with a handful of C++14 extensions. If you are contributing a pull request to the 3.x branch rather than master, your code can’t use C++17 features. Instead, your code must be able to be built with a C++14 compiler.
引擎贡献者的最佳实践.
参见
代码风格指南 for formatting guidelines.
Disallowed features
Any feature not listed below is allowed. Using features likeconstexprvariables andnullptris encouraged when possible. Still, try to keep your use of modern C++ features conservative. Their use needs to serve a real purpose, such as improving code readability or performance.Standard Template Library
STL as Godot provides its own data types (among other things). See 为什么 Godot 不使用 STL(标准模板库)? for more information. not usestd::string,std::vectorand the like. Instead, use Godot’s datatypes as described below: Stringinstead ofstd::string.Vectorinstead ofstd::vector. In some cases,ListorLocalVectorcan be used as an alternative (ask core developers first).Arrayinstead ofstd::array.autokeywordautokeyword for type inference. While it can avoid repetition, it can also lead to confusing code:
Keep in mind hover documentation often isn’t readily available for pull request reviewers. Most of the time, reviewers will use GitHub’s online viewer to review pull requests.// Not so confusing...auto button = memnew(Button);// ...but what about this?auto result = EditorNode::get_singleton()->get_complex_result();
autoinstead of allowing it on a case-by-case basis to avoid having to decide on difficult edge cases. Thank you for your understanding.Lambdas
Lambdas should be used conservatively when they make code effectively faster or simpler, and do not impede readability. Please ask before using lambdas in a pull request.#pragma oncedirective#ifdef-based include guards instead of#pragma oncein new files. 参见 标头引入 for guidelines on sorting includes in C++ and Objective-C files.
