Skip to main content

C++ Naming Conventions

1. Files

snake_case

client/main.cpp
ecs/registry.h

1.1 C++ files extensions

.cpp for source & .h for headers.

main.cpp
main.h

In addition, you must use .tcc when your header file declares a templated type. The .h only declares the type, the implementation is in the .tcc file.

smart_pointer.h # Includes smart_pointer.tcc at the end
smart_pointer.tcc

2. Types

PascalCase

class PlayerController {};
struct Position {};
enum class Direction { North, South };
using Index = std::uint32_t;

3. Functions

snake_case

void update_position(int id);
int get_position(int id);

4. Variables

snake_case

int health_point;
std::string player_name;

5. Prefixes

5.1 Member variables m_

class Player
{
private:
std::string m_name;
int m_health;
};

5.2 Static member variables s_

class Player
{
public:
static std::size_t s_player_count;
};

5.3 Global variables g_

int g_frame_count;

int main()
{
return 0;
}

5.4 Thread-local variable tl_

thread_local std::string log_context;

5.5 Constants k_

constexpr float k_pi = 3.14159f;

5.6 Template parameters T

template <typename T>
class Array {};

template <typename TKey, typename TValue>
class Dictionary {};

5.7 Interfaces class I

IRenderer {
virtual void render() = 0;
};

6. Macros

FULL_CAPS_WITH_UNDERSCORES.

#define LOG_DEBUG(x)
#define ENGINE_VERSION "1.2.0"

7. Namespaces

Short lowercase name.

namespace net { ... } // == networking
namespace io { ... } // == file I/O