This document provides guidelines and best practices for AI agents working on C++ projects. The focus is on Modern C++ (C++11 and newer), which introduced features that make the language safer, more expressive, and easier to use.
A C++ project requires a compiler, a build system, and often a package manager.
- GCC (GNU Compiler Collection): The
g++compiler is the standard on most Linux systems. - Clang: A modern compiler from the LLVM project, known for its excellent error messages.
- MSVC: The Microsoft Visual C++ compiler, standard on Windows.
CMake is the de-facto standard build system for modern C++ projects. It uses a script named CMakeLists.txt to define the build process.
-
CMakeLists.txt: This file contains a set of directives and commands that describe the project's source files and dependencies. -
Basic
CMakeLists.txtExample:# Specify the minimum version of CMake required. cmake_minimum_required(VERSION 3.10) # Set the project name and language. project(MyProject CXX) # Specify the C++ standard. set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Add an executable target. add_executable(my_app main.cpp)
-
Building with CMake:
mkdir build cd build cmake .. make
Managing dependencies in C++ can be complex. Package managers help automate this process.
- vcpkg (Microsoft): A cross-platform package manager.
- Conan: Another popular, cross-platform package manager.
RAII (Resource Acquisition Is Initialization) is a core C++ principle. It means that you tie the life cycle of a resource (like memory, a file handle, or a network socket) to the lifetime of an object. The resource is acquired in the object's constructor and released in its destructor.
Smart Pointers are the primary way to implement RAII for memory management. They automatically handle memory deallocation, preventing memory leaks.
std::unique_ptr: A smart pointer that owns and manages another object through a pointer and disposes of that object when theunique_ptrgoes out of scope. There can only be oneunique_ptrto an object.std::shared_ptr: A smart pointer that retains shared ownership of an object through a pointer. Severalshared_ptrobjects may own the same object. The object is destroyed when the last remainingshared_ptrowning it is destroyed.std::weak_ptr: A smart pointer that holds a non-owning ("weak") reference to an object that is managed by ashared_ptr. It is used to break circular references.
Always prefer smart pointers over raw pointers (new and delete) for managing dynamic memory.
The STL is a set of C++ template classes to provide common data structures and functions.
- Containers:
std::vector: A dynamic array.std::string: A string class.std::map: A key-value map (implemented as a red-black tree).std::unordered_map: A key-value map (implemented as a hash table).
- Algorithms: The
<algorithm>header provides a rich set of functions for operating on ranges of elements (e.g.,std::sort,std::find,std::for_each).
Lambdas are a concise way to create anonymous functions.
#include <vector>
#include <algorithm>
#include <iostream>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
std::for_each(v.begin(), v.end(), [](int n) {
std::cout << n << std::endl;
});
return 0;
}- Google Test (
gtest): A popular and powerful C++ testing framework. It provides a rich set of assertions and features for writing tests. - Catch2: Another popular, modern, and easy-to-use testing framework.
A typical test with Google Test looks like this:
#include "gtest/gtest.h"
int add(int a, int b) {
return a + b;
}
TEST(AddTest, PositiveNumbers) {
EXPECT_EQ(add(2, 3), 5);
}