ISO Cpp Flashcards

1
Q

What is the zero-overhead principle?

A

What you don’t use, you don’t pay for.

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

What it means to express ideas directly in code?

A

What is expressed in code has defined semantics and can (in principle) be checked by the compiler and other tools.

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

What are the enforcements of rule P.1 express ideas directly in code?

A

1) Use const consistently
2) Flag uses of casts (They neuter the type system)
3) Detect code that mimics the standard library (Look for code that can be done with standard library)

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

What are the notes of P.2. ISO Standard C++?

A

1) Avoid dependence on undefined behavior (e.g. undefined order of evaluation)
2) Be aware of construct with implementation defined meaning (e.g. sizeof(int))
3) Using valid ISO C++ does not guarantee portability

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

What are the P.3. Express intent enforcements?

A

1) Simple for loops vs. range-for loops
2) f(T*, int) interfaces vs. f(span) interfaces
3) Loop variables in too large scope
4) Naked new and delete
5) Functions with many parameters of built in types

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

What is the principle and enforcements of P.4: Ideally, a program should be statically type safe?

A

1) Type errors should be catched during compile time rather than runtime
unions – use variant (in C++17)
casts – minimize their use; templates can help
array decay – use span (from the GSL)
range errors – use span
narrowing conversions – minimize their use and use narrow or narrow_cast (from the GSL) where they are necessary

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

P.5: Prefer compile-time checking to run-time checking

A

Code clarity and performance. You don’t need to write error handlers for errors caught at compile time.

Alternative formulation: Don’t postpone to run time what can be done well at compile time.

Enforcement
Look for pointer arguments.
Look for run-time checks for range violations.

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

P.6: What cannot be checked at compile time should be checkable at run time

A

Leaving hard-to-detect errors in a program is asking for crashes and bad results.

Flag (pointer, count)-style interfaces (this will flag a lot of examples that can’t be fixed for compatibility reasons)

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

P.7: Catch run-time errors early

A

Avoid “mysterious” crashes. Avoid errors leading to (possibly unrecognized) wrong results.

Look at pointers and arrays: Do range-checking early and not repeatedly
Look at conversions: Eliminate or mark narrowing conversions
Look for unchecked values coming from an input
Look for structured data (objects of classes with invariants) being converted into strings

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

P.8: Don’t leak any resources

A

Enforcement
Look at pointers: Classify them into non-owners (the default) and owners. Where feasible, replace owners with standard-library resource handles (as in the example above). Alternatively, mark an owner as such using owner from the GSL.
Look for naked new and delete
Look for known resource allocating functions returning raw pointers (such as fopen, malloc, and strdup)

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

P.9: Don’t waste time or space

A

Flag an unused return value from a user-defined non-defaulted postfix operator++ or operator– function. Prefer using the prefix form instead. (Note: “User-defined non-defaulted” is intended to reduce noise. Review this enforcement if it’s still too noisy in practice.)

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

P.10: Prefer immutable data to mutable data

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

P.11: Encapsulate messy constructs, rather than spreading through the code

A

Look for “messy code” such as complex pointer manipulation and casting outside the implementation of abstraction

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

P.12: Use supporting tools as appropriate

A

Run a static analyzer to verify that your code follows the guidelines you want it to follow.

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

P.13: Use support libraries as appropriate

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

What is an interface?

A

An interface is a contract between two parts of a program.

17
Q

I.1: Make interfaces explicit

A

Correctness. Assumptions not stated in an interface are easily overlooked and hard to test.

Alternative formulation: Avoid passing information across an interface through non-local or implicit state. Note that non-const member functions pass information to other member functions through their object’s state.

Alternative formulation: An interface should be a function or a set of functions. Functions can be function templates and sets of functions can be classes or class templates.

Enforcement
(Simple) A function should not make control-flow decisions based on the values of variables declared at namespace scope.
(Simple) A function should not write to variables declared at namespace scope.