Other Coding Rules: Difference between revisions

From OpenMW Wiki
Jump to navigation Jump to search
m (Updating the VS2013 section.)
(2013 is dead. 2015 is dead. Only compliant C++14 toolchains are in use. Rejoice.)
Line 20: Line 20:
* When returning a pointer, don't return a 0-pointer in case of an error (returning a 0-pointer is still valid, if it does not represent an error situation).
* When returning a pointer, don't return a 0-pointer in case of an error (returning a 0-pointer is still valid, if it does not represent an error situation).
* Remember the [https://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29 Rule of Three].
* Remember the [https://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29 Rule of Three].
== Visual Studio 2013 ==
OpenMW currently uses a Visual Studio 2013 build system, to maintain compatibility the following issues have been identified. This build system also supports Windows XP.
*C++11 Core Language Feature constexpr is not supported. [https://msdn.microsoft.com/en-us/library/hh567368.aspx MSDN]
**[https://github.com/OpenMW/openmw/pull/1414#pullrequestreview-58477144 Pull Request #1414 · OpenMW/openmw · GitHub]
**[https://github.com/OpenMW/openmw/pull/1450#pullrequestreview-62409966 Pull Request #1450 · OpenMW/openmw · GitHub]
*Avoid using single-value initialization of plain C-arrays. It is not well defined with C++11.
**[https://github.com/OpenMW/openmw/pull/1450#issuecomment-329764654 Pull Request #1450 · OpenMW/openmw · GitHub]
*C++11 cannot initialize the std::atomic_bool variable by bool "false" value.
**[https://github.com/OpenMW/openmw/pull/1633#pullrequestreview-109188462 Pull Request #1633 · OpenMW/openmw · GitHub]
**[https://stackoverflow.com/a/15751048 Stack Overflow report and workaround.]

Revision as of 15:16, 29 June 2020

Namespaces

We prefer fully qualified names (e.g. std::cout).

Using directives (e.g. "using namespace std") are not welcome. Using directives in headers will result in the code being rejected.

Using declarations (e.g. "using std::cout") are tolerated, but should be used only in the innermost possible scope (i.e. usually function scope). Fully qualified names are still preferred.

Do not declare any names in the global namespace (the only exception to this rule is the file which contains the main() function of an executable).

Other Pieces of Advice

  • Prefer C++ means over C means. In particular:
    • Do not use the preprocessor for purposes other than conditional compiling (this includes include guards)
    • Use C++ streams instead of C I/O
    • Use new/delete instead of malloc/free (but usage of new/delete should also be limited, see below)
  • Prefer STL-container over raw arrays and new[]/delete[].
  • Use new/delete only when necessary. Prefer automatic storage duration.
  • Throw exceptions instead of retuning error codes.
  • When returning a pointer, don't return a 0-pointer in case of an error (returning a 0-pointer is still valid, if it does not represent an error situation).
  • Remember the Rule of Three.