Introduction
A .cpp file holds source code in C++, the language that grew out of
C with classes, templates and a large standard library.
As in C, a compiler turns each source file into an object file, and a linker joins them
into a program or library.
Compilers do not agree on one extension, so several are common: .cpp,
.cc and .cxx for source files, and .hpp,
.hh, .hxx or plain .h for headers. Since C++20,
modules may use extensions such as .cppm or .ixx, depending on
the compiler.
Structure
A C++ source file is plain text with no header or signature. It passes through the preprocessor, and what remains is a series of declarations. A source file with the headers it includes forms one translation unit, and the File structure page explains how these are compiled and linked.
#include <iostream>
#include <string>
int main()
{
std::string name = "world";
std::cout << "Hello, " << name << '\n';
}
Language reference
These pages are a reference to C++23 (ISO/IEC 14882:2024), and follow the C++23 working draft, N4950. Names in square brackets, such as [dcl.init], are the standard’s section names.
- File structure – file extensions, translation units, the phases of translation, encoding, headers, linkage and the one-definition rule
- Lexical structure – tokens, comments, identifiers, keywords by standard, literals, raw strings, user-defined literals and alternative tokens
- Preprocessor – every directive, macros, predefined macros,
__has_includeand feature-test macros - Types – fundamental and compound types,
constandvolatile, references, value categories,auto,decltypeand conversions - Declarations – specifiers, declarators, initialisation, structured bindings,
constexpr,consteval,constinitand attributes - Namespaces and modules – namespaces,
using, name lookup, argument-dependent lookup and C++20 modules - Classes – members, access, special member functions, constructors, destructors and operator overloading
- Inheritance – virtual functions, abstract classes, multiple and virtual inheritance
- Templates – class, function, variable and alias templates, specialisation, deduction, SFINAE and variadics
- Concepts – concept definitions,
requiresand the standard concepts - Functions – default arguments, overload resolution, lambdas and coroutines
- Expressions – the operator table, evaluation order,
newanddelete, and casts - Statements – selection, loops, range-based
for, jumps andtryblocks - Exceptions –
throw, handlers, stack unwinding,noexceptand the standard exception classes - Undefined behaviour – object lifetime, aliasing, the memory model and data races
- Standard library – every header by group, and the standard that added it
- Versions – what C++98, 03, 11, 14, 17, 20 and 23 each added, removed and deprecated
Modules and line continuation in the viewer
module and import are coloured as keywords only where they
begin a declaration line, alone or after export: export module a.b;,
module;, module a:p;, module :private;,
import <vector>;, import "x.h";, import std;,
import :part; and export import x;. Anywhere else (a variable named
module, a.import(b)) they are ordinary names. The check looks
at the same line, so a declaration split over several lines is not coloured.
A backslash that ends a line joins it to the next, also in a // comment, a
string and a preprocessor line, as it does in C (see the
C page).
Not described
Known, not yet covered
- Reference: the standard library page lists every C++23 header by group and the standard that added it, but not the classes, functions and algorithms inside them (
std::vector,std::sortand the rest are named in passing, with no entry of their own). - Reference: C++26 is stated as not covered (versions); its contracts, static reflection and other additions have no page.
- Reference: compiler extensions (GNU and Clang
__attribute__,__builtin_*functions, MSVC__declspec), name mangling and the binary interface are not described (the word ABI appears only on the inheritance page). C++/CLI is a separate dialect with its own family and is not covered by these pages. - Viewer: names in
std::are plain; only built-in type words and names ending in_tare coloured as types, and attribute names inside[[ ]](nodiscard) are plain. - Viewer: the scanner does not evaluate
#if, expand macros or parse templates, so code in a#if 0block is coloured as live. - Viewer: no errors are reported: the viewer does not check names, types, overloads, concepts or template arguments, and does not follow
#includeor module imports. The Includes table lists only#includelines with their file name on the same line. Only the first 1 MiB of a file is read and coloured.
Also used for
- C++/CLI source code – C++ with the CLR extensions for .NET: ref classes, handles, gcnew, properties.
- C++/CLI source code – C++ with the CLR extensions for .NET: ref classes, handles, gcnew, properties.
- C++/CLI source code – C++ with the CLR extensions for .NET: ref classes, handles, gcnew, properties.
- C++/CLI source code – C++ with the CLR extensions for .NET: ref classes, handles, gcnew, properties.
- C++/CLI source code – C++ with the CLR extensions for .NET: ref classes, handles, gcnew, properties.
- C++/CLI source code – C++ with the CLR extensions for .NET: ref classes, handles, gcnew, properties.
Links
Software
- GCC – the GNU compiler collection, with the
g++C++ compiler (Linux, Windows, macOS) - Clang – the LLVM project’s C and C++ compiler (Linux, Windows, macOS)
- Visual Studio – Microsoft’s development environment, with the MSVC compiler (Windows)
- CLion – JetBrains’ development environment for C and C++ (Windows, macOS, Linux)
- Xcode – Apple’s development environment, which builds C++ with Clang (macOS)
- Visual Studio Code – free code editor with C++ extensions (Windows, macOS, Linux)
Developers
- libclang – interface to Clang’s parser, with Python bindings (C)
- tree-sitter-cpp – C++ grammar for the Tree-sitter parser used by editors
Reference
- ISO/IEC JTC1/SC22/WG21 – the committee that maintains the C++ standard, with its working papers
- C++ reference – cppreference.com’s reference to the language and library