Skip to content
Moemod Hymei⭐ edited this page Jun 24, 2020 · 1 revision

C++ Standard

Currently we use C++17. However, due to poor portability, some new features are banned:

  • filesystem
  • execution
  • optional

Scoping

Headers

Every .cpp file should have a .h when something should be exposed to other compile unit.
Implementations with inline and template should be written into .hpp file. We will remove headers as soon as C++20 modules become available.

Unnamed Namespaces and Static Variables

We prefer static than namespace {}.

Functions

Input

Smart pointers should be passed by value. void foo(std::shared_ptr<T>) instead of const std::shared_ptr<T> &.

Output

Never use out ref or ptr to store return data e.g. void VectorAdd(const Vector &a, const Vector &b, Vector &out), just return object instead e.g. Vector VectorAdd(const Vector &a, const Vector &b) An exception is when return type should be inferred from template arguments.

Memory management

Smart pointers

Avoid using plain new and delete. Prefer using make_shared and make_unique.

Exceptions

We don't use exceptions right away. Prefer assert more.

Templates

Template members

Every member-like function in template classes (including friend and static) should be decleared and defined (or implemented) inside the class definition. DO NOT place them outside.
Reduce usage of template member functions in template classes, if they are too awkward to call e.g. obj.template foo<T>()