Constant and Strings Flashcards

1
Q

named constant

A

a constant value that is associated with an identifier

const int magic = 5;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Literal constant

A

a constant value that is not associated with an identifier

“Hello”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

constant expression

A

an expression that can be evaluated at compile-time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

runtime expression

A

any expression that is not a constant expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

compile-time constant

A

is a constant whose value is known at compile-time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

runtime constant

A

a constant whose initialization value isn’t known until runtime

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

constexpr

A

must be a compile-time constant, and initialized with a constant expression. Function parameters cannot be constexpr

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Inline expansion

A

a process where a function call is replaced by the code from the called function’s definition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does an inline function request from the compiler?

Give code example of inline function

A

A function that the compiler is requested to expand in-line at each point the function is called, instead of performing a regular function call. The compiler may choose to ignore this request

inline int add(int a, int b) {
return a + b;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Two primary requirements of inline functions

A

The compiler needs to be able to see the full definition of an inline function or variable in each translation unit where the function is used (a forward declaration will not suffice on its own). The definition can occur after the point of use if a forward declaration is also provided.

Every definition for an inline function or variable must be identical, otherwise undefined behavior will result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

inline variables

A

variables that are allowed to be defined in multiple files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what does the term inline mean in modern c++

A

multiple definitions are allowed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

constexpr function

A

function whose return value may be computed at compile-time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When will constexpr functions be evaluated

A

Constexpr functions are only guaranteed to be evaluated at compile-time when used in a context that requires a constant expression. Otherwise they may be evaluated at compile-time (if eligible) or runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

consteval function

A

a function that must evaluate at compile-time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

std::string_view

A

provides read-only access to an existing string (a C-style string literal, a std::string, or a char array) without making a copy

17
Q

issues with std::string_view

A

A std::string_view that is viewing a string that has been destroyed is sometimes called a dangling view.

When a std::string is modified, all views into that std::string are invalidated, meaning those views are now invalid. Using an invalidated view (other than to revalidate it) will produce undefined behavior.

18
Q

std::string

A

offers an easy and safe way to deal with text strings. std::string lives in the string header. std::string is expensive to initialize (or assign to) and copy.

19
Q

Why can you initialise std::string_view with a literal?

A

Because C-style string literals exist for the entire program, it is okay to set a std::string_view to a C-style string literal, and even return such a std::string_view from a function.