Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Type system

The C++ type system defines the types of objects, references, functions, and expressions, together with permitted operations and conversions. Typing is static: auto deduces a type but does not let a variable change type later. Implicit conversions may be allowed even when they risk losing information.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

T name = initializer;
T name{initializer};
auto name = initializer;
const T name = initializer;
T& reference = object;

The type system (type system) is the set of rules describing types and their use. C++ is statically typed: a variable's type is established during compilation, and assigning a new value does not change it. The type influences permitted operations and conversions. Passing type checks alone does not guarantee safe program execution.

Types are divided into fundamental types, such as int, double, bool, and void, and compound types, such as arrays, pointers, references, functions, classes, and enumerations. A reference type is not an object type; a reference provides a way to refer to an existing object. const restricts modification of an object through a particular name or access path; it does not automatically mean a compile-time constant.

In auto total = count + 2;, the compiler deduces int. auto does not mean dynamic typing. decltype obtains a type from an expression without evaluating it; for an ordinary variable, decltype(total) gives its declared type. In the example, alias refers to total, so alias += 1 changes that same object.

C++ permits implicit conversions: here, the int value 6 is exactly representable as double. Not every conversion preserves information. List-initialization prohibits narrowing conversions, so uncommenting int invalid{2.5}; produces a compilation error rather than a runtime check.

This entry targets C++23. The supplied standard URL serves an evolving working draft, not a frozen C++23 publication; this description is limited to rules available in C++23. A compiler flag does not attest behavior in a production runtime.

Examples

#include <iostream>

int main() {
    const int count{3};
    auto total = count + 2;
    int& alias = total;
    alias += 1;
    double widened = total;
    // int invalid{2.5};
    std::cout << total << ' ' << widened << '\n';
}

Result

6 6

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources