2
0

cpp_usage_guidelines.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. .. _doc_cpp_usage_guidelines:
  2. C++ usage guidelines
  3. ====================
  4. Rationale
  5. ---------
  6. Since Godot 4.0, the C++ standard used throughout the codebase is a subset of
  7. **C++17**. While modern C++ brings a lot of opportunities to write faster, more
  8. readable code, we chose to restrict our usage of C++ to a subset for a few
  9. reasons:
  10. - It makes it easier to review code in online editors. This is because engine
  11. contributors don't always have access to a full-featured IDE while reviewing
  12. code.
  13. - It makes the code easier to grasp for beginner contributors (who may not be
  14. professional C++ programmers). Godot's codebase is known to be easy to learn
  15. from, and we'd like to keep it that way.
  16. To get your pull request merged, it needs to follow the C++ usage guidelines
  17. outlined here. Of course, you can use features not allowed here in your own C++
  18. modules or GDExtensions.
  19. .. note::
  20. Prior to Godot 4.0, the C++ standard used throughout the codebase was C++03,
  21. with a handful of C++14 extensions. If you are contributing a pull request
  22. to the `3.x` branch rather than `master`, your code can't use C++17 features.
  23. Instead, your code must be able to be built with a C++14 compiler.
  24. The guidelines below don't apply to third-party dependencies, although we
  25. generally favor small libraries instead of larger solutions. See also
  26. :ref:`doc_best_practices_for_engine_contributors`.
  27. .. seealso::
  28. See :ref:`doc_code_style_guidelines` for formatting guidelines.
  29. Disallowed features
  30. -------------------
  31. **Any feature not listed below is allowed.** Using features like ``constexpr``
  32. variables and ``nullptr`` is encouraged when possible. Still, try to keep your
  33. use of modern C++ features conservative. Their use needs to serve a real
  34. purpose, such as improving code readability or performance.
  35. .. _doc_cpp_godot_types:
  36. Standard Template Library
  37. ~~~~~~~~~~~~~~~~~~~~~~~~~
  38. We don't allow using the `STL <https://en.wikipedia.org/wiki/Standard_Template_Library>`__
  39. as Godot provides its own data types (among other things).
  40. See :ref:`doc_faq_why_not_stl` for more information.
  41. This means that pull requests should **not** use ``std::string``,
  42. ``std::vector`` and the like. Instead, use Godot's datatypes as described in
  43. the :ref:`doc_core_types` documentation.
  44. A 📜 icon denotes the type is part of :ref:`Variant <doc_variant_class>`. This
  45. means it can be used as a parameter or return value of a method exposed to the
  46. scripting API.
  47. ``auto`` keyword
  48. ~~~~~~~~~~~~~~~~
  49. Please don't use the ``auto`` keyword for type inference. While it can avoid
  50. repetition, it can also lead to confusing code:
  51. .. code-block:: cpp
  52. // Not so confusing...
  53. auto button = memnew(Button);
  54. // ...but what about this?
  55. auto result = EditorNode::get_singleton()->get_complex_result();
  56. Keep in mind hover documentation often isn't readily available for pull request
  57. reviewers. Most of the time, reviewers will use GitHub's online viewer to review
  58. pull requests.
  59. The ``auto`` keyword can be used in some special cases, like C++ lambda or Objective-C block
  60. definitions and C++ templates. Please ask before using templates with ``auto`` in a pull request.
  61. .. code-block:: cpp
  62. // Full type definitions.
  63. void (*mult64to128)(uint64_t, uint64_t, uint64_t &, uint64_t &) = [](uint64_t u, uint64_t v, uint64_t &h, uint64_t &l) { ... }
  64. void (^JOYSTICK_LEFT)(GCControllerDirectionPad *__strong, float, float) = ^(GCControllerDirectionPad *dpad, float xValue, float yValue) { ... }
  65. // Less clutter with auto.
  66. auto mult64to128 = [](uint64_t u, uint64_t v, uint64_t &h, uint64_t &l) { ... }
  67. auto JOYSTICK_LEFT = ^(GCControllerDirectionPad *dpad, float xValue, float yValue) { ... }
  68. // Compare function for different types.
  69. template <typename T1, typename T2>
  70. constexpr auto MIN(const T1 m_a, const T2 m_b) {
  71. return m_a < m_b ? m_a : m_b;
  72. }
  73. We chose to forbid ``auto`` in all other cases. Thank you for your understanding.
  74. Lambdas
  75. ~~~~~~~
  76. Lambdas should be used conservatively when they make code effectively faster or
  77. simpler, and do not impede readability. Please ask before using lambdas in a
  78. pull request.
  79. ``#ifdef``-based include guards
  80. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  81. Starting with 4.5, all files now use the ``#pragma once`` directive, as they
  82. improve readability and declutter macros. Use of ``#ifdef``-based include
  83. guards are now actively discouraged.
  84. ``try``-``catch`` blocks
  85. ~~~~~~~~~~~~~~~~~~~~~~~~
  86. C++ style exception handling using ``try`` and ``catch`` blocks is forbidden.
  87. This restriction is in place for several reasons, including performance, binary
  88. size and code complexity.
  89. Use :ref:`doc_common_engine_methods_and_macros_error_macros` instead.
  90. .. seealso::
  91. See :ref:`doc_code_style_guidelines_header_includes` for guidelines on sorting
  92. includes in C++ and Objective-C files.