Skip to content

Add a table listing STL datatypes and their Godot equivalents in C++ usage guidelines #10873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 55 additions & 12 deletions contributing/development/cpp_usage_guidelines.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,61 @@ as Godot provides its own data types (among other things).
See :ref:`doc_faq_why_not_stl` for more information.

This means that pull requests should **not** use ``std::string``,
``std::vector`` and the like. Instead, use Godot's datatypes as described below:

- Use ``String`` instead of ``std::string``.
- Use ``Vector`` instead of ``std::vector``. In some cases, ``LocalVector``
can be used as an alternative (ask core developers first).
- Use ``Array`` instead of ``std::array``.

.. note::

Godot also has a List datatype (which is a linked list). While List is already used
in the codebase, it typically performs worse than other datatypes like Vector
and Array. Therefore, List should be avoided in new code unless necessary.
``std::vector`` and the like. Instead, use Godot's datatypes as described below.
A 📜 icon denotes the type is part of :ref:`Variant <doc_variant_class>`. This
means it can be used as a parameter or return value of a method exposed to the
scripting API.

+------------------------+--------------------------+--------------------------------------------------------------------------------------+
| Godot datatype | Closest C++ STL datatype | Comment |
+========================+==========================+======================================================================================+
| ``String`` 📜 | ``std::string`` | **Use this as the "default" string type.** ``String`` uses UTF-32 encoding |
| | | to improve performance thanks to its fixed character size. |
+------------------------+--------------------------+--------------------------------------------------------------------------------------+
| ``StringName`` 📜 | ``std::string`` | Uses string interning for fast comparisons. Use this for static strings that are |
| | | referenced frequently and used in multiple locations in the engine. |
+------------------------+--------------------------+--------------------------------------------------------------------------------------+
| ``Vector`` | ``std::vector`` | **Use this as the "default" vector type.** Uses copy-on-write (COW) semantics. |
| | | This means it's generally slower but can be copied around almost for free. |
+------------------------+--------------------------+--------------------------------------------------------------------------------------+
| ``LocalVector`` | ``std::vector`` | Closer to ``std::vector`` in semantics. In most situations, ``Vector`` should be |
| | | preferred. |
+------------------------+--------------------------+--------------------------------------------------------------------------------------+
| ``Array`` 📜 | ``std::vector`` | Values can be of any Variant type. No static typing is imposed. |
| | | Uses shared reference counting, similar to ``std::shared_ptr``. |
+------------------------+--------------------------+--------------------------------------------------------------------------------------+
| ``TypedArray`` 📜 | ``std::vector`` | Subclass of ``Array`` but with static typing for its elements. |
| | | Not to be confused with ``Packed*Array``, which is internally a ``Vector``. |
+------------------------+--------------------------+--------------------------------------------------------------------------------------+
| ``Packed*Array`` 📜 | ``std::vector`` | Alias of ``Vector``, e.g. ``PackedColorArray = Vector<Color>``. |
| | | Only a limited list of packed array types are available |
| | | (use ``TypedArray`` otherwise). |
+------------------------+--------------------------+--------------------------------------------------------------------------------------+
| ``List`` | ``std::list`` | Linked list type. Generally slower than other array/vector types. Prefer using |
| | | other types in new code, unless using ``List`` avoids the need for type conversions. |
+------------------------+--------------------------+--------------------------------------------------------------------------------------+
| ``Span`` | ``std::span`` | Represents read-only access to a contiguous array without needing to copy any data. |
| | | See `pull request description <https://github.com/godotengine/godot/pull/100293>`__ |
| | | for details. |
+------------------------+--------------------------+--------------------------------------------------------------------------------------+
| ``HashSet`` | ``std::unordered_set`` | **Use this as the "default" set type.** |
+------------------------+--------------------------+--------------------------------------------------------------------------------------+
| ``RBSet`` | ``std::unordered_set`` | Uses a `red-black tree <https://en.wikipedia.org/wiki/Red-black_tree>`__ |
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
| ``RBSet`` | ``std::unordered_set`` | Uses a `red-black tree <https://en.wikipedia.org/wiki/Red-black_tree>`__ |
| ``RBSet`` | ``std::set`` | Uses a `red-black tree <https://en.wikipedia.org/wiki/Red-black_tree>`__ |

This was correctly in that sense, also I think a description of RBMap is needed as well, might also want to describe VSet and VMap, which are equivalent to std::flat_set/map (which are in C++23, or in Boost)

Copy link
Member

Choose a reason for hiding this comment

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

VSet and VMap also notably use copy-on-write semantics. But they aren't used much in the engine at all, and the benefit is unclear, so it has been proposed to remove them again.
I guess there's no harm in covering them since they do exist, but I'd probably attach a note that the benefits aren't clearly established.

| | | for faster access. |
+------------------------+--------------------------+--------------------------------------------------------------------------------------+
| ``HashMap`` | ``std::unordered_map`` | **Use this as the "default" map type.** Does not preserve insertion order. |
+------------------------+--------------------------+--------------------------------------------------------------------------------------+
| ``AHashMap`` | ``std::unordered_map`` | Array-based implementation of a hash map. Does not preserve insertion order. |
+------------------------+--------------------------+--------------------------------------------------------------------------------------+
| ``OAHashMap`` | ``std::unordered_map`` | Does not preserve insertion order. |
+------------------------+--------------------------+--------------------------------------------------------------------------------------+
| ``Dictionary`` 📜 | ``std::unordered_map`` | Keys and values can be of any Variant type. No static typing is imposed. |
| | | Uses shared reference counting, similar to ``std::shared_ptr``. |
+------------------------+--------------------------+--------------------------------------------------------------------------------------+
| ``TypedDictionary`` 📜 | ``std::unordered_map`` | Subclass of ``Dictionary`` but with static typing for its keys and values. |
+------------------------+--------------------------+--------------------------------------------------------------------------------------+
| ``Pair`` | ``std::pair`` | Stores a single key-value pair. |
+------------------------+--------------------------+--------------------------------------------------------------------------------------+

``auto`` keyword
~~~~~~~~~~~~~~~~
Expand Down
Loading