General Flashcards

1
Q

What are some of the early C++ standards?

A

C++98 (the first) and C++03 (technical updates). C++11, C++14, C++17.

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

What are some of the major features of C++11

A

C++11 introduced move semantics, variadic templates, initializer lists, constexpr, range-based loops, etc..

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

What are some of the major features of C++14?

A

C++14 introduced std::make_unique, return type deduction, decltype(auto), generic lambda expressions, etc..

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

What is a high level difference between static_cast and dynamic_cast?

A

static_cast is a compile-time check. dynamic_cast is a runtime check using RTTI (runtime type information).

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

What are the 5 value categories and their major attributes?

A

prvalue (pure rvalue - no identity and can be moved from), xvalue (eXpiring value - has identity and can be moved from), lvalue (has identity and can’t be moved from). A glvalue (generalized lvalue) han an identitiy and is either an lvalue or an xvalue. An rvalue is movable and is either a prvalue or an an xvalue.

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

What does “cv-qualified” mean?

A

Having the ‘const’ or ‘volatile’ type qualifiers.

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

What are “storage class specifiers”?

A

Together with the scope of the name, they control two independent properties of the name: its storage duration and its linkage. They are: auto, static, extern, thread_local. (register is deprecated).

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

What is a “function-local predefined variable”

A

Within the function body, the function-local predefined variable __func__ has block scope and static storage duration, and is defined as: static const char __func__[] = “function-name”;

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

What is in “class scope”?

A

The potential scope of a name declared in a class begins at the point of declaration and includes the rest of the class body and all function bodies (even if defined outside the class definition or before the declaration of the name), default arguments, exception specifications, in-class brace-or-equal initializers, contract conditions (since C++20), and all these things in nested classes, recursively.

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

What is the difference between “function parameter scope” and “function scope”?

A

Function scope is for labels. Labels are in scope throughout the entire function body (even before declaration). Function parameter scope begins at declaration, and ends at the end of the containing declaration.

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

What is namespace scope?

A

The potential scope of any entity declared in a namespace begins at the declaration and consists of the concatenation of all namespace definitions for the same namespace name that follow, plus, for any using-directive that introduced this name or its entire namespace into another scope, the rest of that scope.

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

What is the difference between a scoped and unscoped enumeration?

A

Unscoped (“enum color {red, blue};”) introduces names (red, blue) that are in scope after the end of the declaration. Scoped enums (“enum class color {red, blue = red + 2};”), have members only in scope within the declaration, and must be qualified (“color::red”) after the declaration.

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

What is a “function-try-block” and it’s main purpose?

A

Adds a try/catch around an entire body. The try is before the member initializers for a constructor. Any exception is automatically re-thrown after the handler for constructors/destructors. The main use is to respond to an exception thrown from the member initializer list in a constructor by logging and rethrowing, modifying the exception object and rethrowing, throwing a different exception instead, or terminating the program. They are rarely used with destructors or with regular functions.

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

Why must class and struct definitions end with a semi-colon?

A

As an optional list of instances may be declared after the body, e.g. “struct foo {int a; int b;} x, y, z;”

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

What is the value category of the parameter ‘w’ in the following: void f(Widget&& w);

A

A parameter is always an lvalue, even if its type is an rvalue reference.

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

What do std::move and std::forward do at runtime?

A

They generate no code. The are merely function templates that perform casts. std::move unconditionally casts its argument to an rvalue, while std::forward performs this cast only if a particular condition is fulfilled.

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

What is wrong with this code? ctor(const std::string text)

: value(std::move(text)) {…}

A

Param ‘text’ is declared as ‘const’, so it cannot be moved from. (It will compile, but copy construct ‘value’).

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

What is a “universal reference”?

A

It can bind to rvalues or lvalues. If a function template parameter has a type “T&&” for a deduced type “T”, or an object is declared using “auto&&” it is a universal reference.

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

What is wrong with the following code?: void f(char name){/…*/}; f(“test”);

A

In C++, you cannot pass a string literal for a char *. The parameter would need to be “const char *”.

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

What produces an xvalue expression?

A

std::move, and the result of calling a function whose return type is an rvalue reference is an xvalue.

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

In what state are standard library objects after std::move?

A

A valid but unspecified state. That is, only the functions without preconditions, such as the assignment operator, can be safely used on the object after it was moved from.

22
Q

What are some examples of prvalues?

A

The result of calling a function whose return type is not a reference is a prvalue. The value of a literal such as 12, 7.3e5, or true is also a prvalue.

23
Q

What are some examples of lvalues?

A

If E is an expression of pointer type, then *E is an lvalue expression referring to the object or function to which E points. As another example, the result of calling a function whose return type is an lvalue reference is an lvalue.

24
Q

What do “##”, “#”, and “#@” mean in a macro?

A

The “token-pasting operator” (##), joins the tokens either side into one. The “stringizing operator” (#) encloses its argument in double quotes. The “charizing operator” (#@) encloses its argument in single quotes.

25
Q

What are “intrinsic functions” (e.g. _InterlockedCompareExchange and memcpy) and the “/Oi” option?

A

Most functions are contained in libraries, but some functions are built in (that is, intrinsic) to the compiler. These are referred to as intrinsic functions or intrinsics. The code is usually inserted inline, avoiding the overhead of a function call and allowing highly efficient machine instructions to be emitted for that function.

26
Q

What is the C runtime library?

A

The CRT is the part of the C++ Standard Library that incorporates the ISO C99 standard library.

27
Q

What does the UCRT contain?

A

The Universal CRT (UCRT) contains the functions and globals exported by the standard C99 CRT library.

28
Q

What is static linking of the CRT and what is its benefit?

A

This links the library object code directly into the application. This creates a single binary without a DLL dependency, so that you don’t have to deploy the Visual C++ library files separately.

29
Q

Should you use C++ interfaces between separately compiled components?

A

The Visual C++ compiler ABI have historically changed between major compiler releases. This is especially the case for STL containers, where container sizes have varied a lot between compiler releases. Microsoft therefore recommends against using C++ interfaces at module boundaries when one wants to enable client code compiled using a different compiler version. Instead of C++, Microsoft recommends using C or COM interfaces, which are designed to have a stable ABI between compiler releases.

30
Q

Should you throw exceptions from constructors or destructors?

A

Yes for constructors - there’s no other good way to communicate failure. No for destructors - these may be running as an effect of handling another exception. C++ will “terminate()” if an exception is thrown from a destructor.

31
Q

Do you have try/catch/finally in C++?

A

There is no ‘finally’. Use RAII to ensure resources are cleaned up.

32
Q

What is RAII?

A

Resource acquisition is initialization. This idiom ties management of such resources to the lifespan of automatic variables.

33
Q

What value type of exception should you throw?

A

You can throw any type, though deriving from std::exception is preferred. (Some are defined in

34
Q

What are the 3 types of exception guarantees?

A

“No fail guarantee” - will not throw”. “Strong guarantee” - if a function throws an exception no memory leak will occur and program state is unmodified (i.e commit or rollback semantics). “Basic guarantee” - if an exception occurs, no memory leak will occur and the program is still in a valid/usable state.

35
Q

What happens if a ctor throws an exception? Does the dtor run?

A

It does not run. Automatic variables initialized before the exception will have their destructors invoked.

36
Q

What’s the difference between defining UNICODE and _UNICODE?

A

UNICODE affects the Windows APIs, _UNICODE the standard C/C++ APIs. You need to define UNICODE to get the right mapping to the A or W versions of the Win32 APIs. You need _UNICODE to map the _tsc versions or the CRT APIs to the str or wcs* APIs.

37
Q

What does including give you?

A

A bunch of _t* and _T definitions to map to either ascii or unicode APIs, depending on if _UNICODE is defined.

38
Q

What’s the difference between the _T and _TEXT macros?

A

Nothing, they both map to the __T macro, which maps __T(x) to L##x if _UNICODE is defined, else just to x.

39
Q

How do you define a unicode entry point for your app?

A

Defined a ‘wmain’ function that takes a ‘wchar_t *argv’ param. (Or use ‘_tmain’ with a _TCHAR *argv if using tchar.h).

40
Q

What are the underscore rules in identifier naming?

A

Don’t define names at global scope that being with an “_”, these are reserved for the compiler and standard library. Don’t define names at any scope beginning with a double underscore, or underscore followed by an uppercase letter, there are reserved for any use.

41
Q

Describe the cdecl calling convention.

A

__cdecl is the default calling convention for C and C++ programs. Because the stack is cleaned up by the caller, it can do vararg functions. The __cdecl calling convention creates larger executables than __stdcall, because it requires each function call to include stack cleanup code.

42
Q

How are cdecl names decorated?

A

Underscore character (_) is prefixed to names, except when __cdecl functions that use C linkage are exported.

43
Q

What is the stdcall calling convention?

A

The __stdcall calling convention is used to call Win32 API functions (WINAPI). The callee cleans the stack, so the compiler makes vararg functions __cdecl.

44
Q

How are stdcall names decorated?

A

An underscore (_) is prefixed to the name. The name is followed by the at sign (@) followed by the number of bytes (in decimal) in the argument list.

45
Q

What is the x64 calling convention?

A

Given the expanded register set, x64 uses the __fastcall calling convention and a RISC-based exception-handling model. The __fastcall convention uses registers for the first four arguments and the stack frame to pass additional arguments. Note that in a 64-bit environment, functions are not decorated.

46
Q

What is the thiscall calling convention?

A

The __thiscall calling convention is used on member functions and is the default calling convention used by C++ member functions that do not use variable arguments. Under __thiscall, the callee cleans the stack, which is impossible for vararg functions. Arguments are pushed on the stack from right to left, with the this pointer being passed via register ECX, and not on the stack, on the x86 architecture. vararg member functions use the __cdecl calling convention. All function arguments are pushed on the stack, with the this pointer placed on the stack last.

47
Q

How are __thiscall names decorated?

A

Because this calling convention applies only to C++, there is no C name decoration scheme. C++ names are decorated in an internal-only manner that includes an encoding for class, namespace, param types, return type, etc. “void __thiscall a::func1(int)” would be something like “?func1@a@@AAEXH@Z”.

48
Q

How would you enable class C to be implicitly converter to an int?

A

Define a type conversion operator member, e.g. “operator int() {…}”

49
Q

How can implicit type conversion occur, and how can you prevent it?

A

A will implicitly convert to B if A has a type conversion operator (e.g. “A::operator B();”), or B has a constructor that takes one parameter of type A (e.g. “B(A src);”). Marking such members “explicit” will prevent the implicit conversion.

50
Q

What must you do if providing the move or copy assignment operations?

A

Also provide the corresponding move or copy constructors.

51
Q

Should you generally provide move operations on a copyable type?

A

Move operations for copyable types are strictly a performance optimization and are a potential source of bugs and complexity, so avoid defining them unless they are significantly more efficient than the corresponding copy operations.