C++ Keywords Reference
Keywords are the words C++ reserves for itself. You cannot use them as variable or function names - the language already owns them. This is your lookup table for the most common ones.
Type keywords
| Keyword | Purpose |
|---|
int | declares a whole-number (integer) variable |
float | declares a decimal number (single precision) |
double | declares a decimal number (double precision) |
short | integer, usually smaller than int |
long | integer, usually larger than int |
char | a single character, like 'A' |
bool | a true/false value |
auto | lets the compiler figure out the type for you |
void | "no type" - used for functions that return nothing |
const | marks a value that must not change |
constexpr | a value computed at compile time (C++11+) |
| Keyword | Purpose |
|---|
struct | groups data fields into one type |
union | a type where all fields share one memory spot |
enum | a set of named constants |
unsigned | an integer type that holds only 0 and above |
signed | an integer type that can be negative (default) |
bool | a true/false value (same as bool) |
string | a sequence of characters (from the std library) |
Control-flow keywords
| Keyword | Purpose |
|---|
if | run code only when a condition is true |
else | run the alternative branch when if was false |
else if | chain a new condition after a false if |
switch | pick one of many branches based on a value |
case | one branch inside a switch |
default | the "else" branch of a switch |
for | loop with a start, condition, and step |
while | loop while a condition is true |
do | loop once, then keep looping while a condition is true |
break | leave the nearest loop or switch early |
continue | skip the rest of this iteration of a loop |
return | send a value back from a function and stop it |
goto | jump to a labeled line (avoid it) |
Class and object keywords
| Keyword | Purpose |
|---|
class | define a type with data and functions |
struct | like a class, but members are public by default |
public | members accessible from outside the class |
private | members accessible only inside the class |
protected | members accessible inside the class and its subclasses |
this | a pointer to the current object inside a class |
new | allocate memory on the heap and build an object |
delete | free memory created with new |
virtual | allow a member function to be overridden by subclasses |
friend | grant an outside function/class access to private members |
template | write code that works with any type |
typename | announces a generic type parameter in a template |
operator | define how an operator behaves for your class |
inline | hint that a function should be inlined at the call site |
explicit | stop the compiler from using a constructor implicitly |
override | state that a function overrides a base-class one (C++11) |
final | stop a class, function, or virtual from being further overridden (C++11) |
Namespace and linker keywords
| Keyword | Purpose |
|---|
namespace | group names under one label |
using | using namespace std; pulls names into scope |
extern | declare a variable/function defined in another file |
inline | suggest the function be inlined at each call |
auto | let the compiler deduce the type of a variable |
throw | raise an exception |
try | start a block that may catch exceptions |
catch | handle an exception thrown in a try block |
typename | name a dependent type inside a template |
register | ask the compiler to keep a variable in a CPU register (deprecated by C++17) |
volatile | tell the compiler a variable may change from outside the program |
mutable | a member that may change even inside a const object |
Notes
- Don't name your variables, functions, or classes with any of these - the compiler will reject them (or behave badly).
static shows up in three places with three meanings: file-local variable, class member owned by the class, and storage inside a function. The trick is reading the surrounding context.const and constexpr both mean "won't change"; constexpr upgrades that to "computed before the program even runs".- The
throw / try / catch words power exception handling. Seeing throw means "something went wrong here - pass control to a catcher".
TL;DR
- Keywords are reserved words the language owns as its own names.
- Biggest groups: type keywords, control-flow keywords, class keywords.
void means "no value", const means "read-only value".- Never use a keyword for variable or function names.