Variables and Basic Types

C++ keywords & alternative operator names:

alignas alignof and and_eq asm 
auto bitand bitor bool break
case catch char char8_t char16_t
char32_t class compl concept const 
const_cast consteval constexpr constinit continue 
co_await co_return co_yield decltype default
delete do double dynamic_cast else 
enum explicit export extern false
float for friend goto if 
inline int long mutable namespace 
new noexcept not not_eq nullptr 
operator or or_eq private protected 
public register reinterpret_cast requires return 
short signed sizeof static static_assert
static_cast struct switch template this
thread_local throw true try typedef 
typeid typename union unsigned using 
declaration using directive virtual void 
volatile wchar_t while xor xor_eq

Compound Type

A declaration is a base type followed by a list of declarators.

References

When we define a reference, we bind the reference to its initializer. There is no way to rebind a reference to refer to a different object.

Two exceptions to the rule that the type of a reference must match the type of the object to which it refers:

  • We can initialize a reference to const from any expression that can be converted

Pointers

Null pointers:

A void* pointer holds an address, but the type of the object at that address is unknown.

Discussion

A reference is not an object such that we may not have a pointer to a reference.

A pointer is an object, we can define a reference to a pointer.

Suggestion: understand pointer or reference declarations by reading from right to left.

const Qualifier

Because we can’t change the value of a const object after we create it, it must be initialized.

By default, const objects are local to a file.

To define a single instance of a const variable that can be shared acrossed files, we use the keyword extern on both its definition and declaration(s):

References to const

Because we cannot assign directly to ci, we also should not be able to use a reference to change ci.

reference to const == const reference

We can initialize a reference to const from any expression that can be converted

The code is transdformed by the compiler into:

A reference to const restricts only what we can do through that reference. Binding a reference to const to an object says nothing about whether the underlying object itself is const.

Pointers and const

const Pointers

const pointer != pointer to const

Like any other const object, a const pointer must be initialized, and once initialized, its value (i.e., the address that it holds) may not be changed.

Top-Level const

We use the term top-level const to indicate that the pointer itself is a const. When a pointer can point to a const object, we refer to that const as a low-level const.

constexpr and Constant Expressions

A constant expression is an expression whose value cannot change and that can be evaluated at compile time.

Under the new standard, we can ask the compiler to verify that a variable is a constant expression by declaring the variable in a constexpr declaration. Variables declared as constexpr are implicitly const and must be initialized by constant expressions.

The types we can use in a constexpr are known as “literal types” because they are simple enough to have literal values.

We can initialize a constexpr pointer from the nullptr literal or the literal (i.e., constant expression) 0. We can also point to (or bind to) an object that remains at a fixed address (an object defined outside of any function).


It is important to understand that when we define a pointer in a constexpr declaration, the constexpr specifier applies to the pointer, not the type to which the pointer points:

Dealing with Types

Type Aliases

The new standard introduced a second way to define a type alias, via an alias declaration:

**Do not intepreting a declaration that uses a type alias by conceptually replacing the alias with its corresponding type. **

The auto Type Specifier

Compound Types, const and auto

auto ordinarily ignores top-level consts.

If we want the deduced type to have a top-level const, we must say so explicitly:

We can also specify that we want a reference to the auto-deduced type. When we ask for a reference to an auto-deduced type, top-level consts in the initializer are not ignored.

The decltype Type Specifier

Here, the compiler does not call f, but it uses the type that such a call would return as the type for sum.

When the expression to which we apply decltype is a variable, decltype returns the type of that variable, including top-level const and reference:

decltype and References

Generally speaking, decltype returns a reference type for expressions that yield objects that can stand on the left-hand side of the assignment.

The above rule only apply to EXPRESSIONs. For example, i is an variable while i+0 and (i) are expressions.

The dereference operator is an example of an expression for which decltype returns a reference.

Last updated