Sign in with Google to keep private notes

Declaration Rules in C++

C++ Declarations — Quick Reference

Taken from Ben Saks lecture on Declarations (CppCon 2022)

1. Declarations vs. definitions

A declaration introduces a name and its properties. A definition provides the entity itself. Every definition is a declaration, but not vice versa.

int x;               // Object definition: storage allocated
extern int y;        // Declaration only: definition elsewhere
extern int z = 42;   // Definition: has an initializer

int f(int);          // Function declaration
int f(int n) { return n + 1; } // Function definition

2. Declaration anatomy

static unsigned long int *p[N];
// |-- specifiers ----| |declarator|
  • The declarator (*p[N]) contains the name and builds its type: p is an array of N pointers to unsigned long int. There is the id-declarator (the name of the thing) and the operator-declarator (* for points, [N] for indexing)
  • Declaration specifiers describe the base type (unsigned long int) or properties such as storage duration/linkage (static, extern). Their order does not matter when the combination is legal. There are type specifiers and non-type specifiers. Type specifiers are commutative and determine the type of the specific entity but non-type specifiers act on the specific id-declarator (const, volatile etc).

3. Reading declarators

Start at the identifier, then follow precedence outward:

Grouping () > array [] / function () > pointer * / references &, &&.

The following lines are separate examples, not one set of declarations to compile together.

int *f(int);         // Function(int) returning int*
int (*f)(int);       // Pointer to function(int) returning int
int *a[5];          // Array of 5 pointers to int
int (*a)[5];        // Pointer to array of 5 ints
int *(*g[3])(double); // Array of 3 function pointers; each returns int*

For chains of pointers, reading leftward from the identifier is a useful shortcut. Parentheses, arrays and functions require the precedence rule.

Note: as it appears so regularly, const * is itself a token to C++'s compiler.

4. const, volatile, constexpr

int const *p;             // Pointer to const int
int *const q = nullptr;   // Const pointer to int
int const *const r = nullptr; // Const pointer to const int
int *const *s;            // Pointer to const pointer to int
  • const / volatile among the specifiers qualify the base type; immediately after *, they qualify that pointer.
  • const int and int const mean the same thing.
  • constexpr is a declaration specifier that applies to the declared object, not the pointee; its initializer must be a constant expression.
constexpr int *p = nullptr; // p is a const pointer to mutable int
// Type behaves like: int *const p

5. Initializers

int a = 42;   // Equals initialization
int b(42);    // Parenthesized initialization
int c{42};    // Braced initialization

One declaration can have multiple declarators (int n = 42, *p = &n;), but separate declarations are generally clearer.

6. Dependent names and typename

A dependent name depends on a template parameter. The compiler parses a template before it knows what type will replace T.

template <class T>
void foo() {
    typename T::size_type *p = nullptr;
}

Without typename, T::size_type * p could be interpreted as multiplication, because T::size_type might name a value instead of a type. typename says it names a type.

  • template <class T> and template <typename T> are equivalent.
  • Only typename (not class) disambiguates T::size_type in this context.

7. Deduction and forwarding references

An unqualified T&& is a forwarding reference when T is deduced from the argument:

template <class T>
void relay(T&& x) {
    process(std::forward<T>(x));
}

int n = 1;
relay(n);  // T = int&; T&& collapses to int&
relay(1);  // T = int;  parameter is int&&
  • Reference collapsing: & combined with any reference becomes &; && + && becomes &&.
  • Named x is an lvalue expression, even when its type is T&&; std::forward<T>(x) preserves the caller's lvalue/rvalue category for overload selection.
  • If T is already fixed (e.g. a member of class C<T>), T&& is not a forwarding reference. T const&& is not one either.

8. Bonus: the most vexing parse

date d(month(m), day(n), year(y)); // May be parsed as a function declaration
date d{month(m), day(n), year(y)}; // Unambiguously an object

A single type such as size_t for month, day and year is a type sink: swapped arguments still compile. Distinct month, day, year types—and optionally user-defined literals like 10_month—make the intent explicit.

Articles to read next