General Flashcards
What are some of the early C++ standards?
C++98 (the first) and C++03 (technical updates). C++11, C++14, C++17.
What are some of the major features of C++11
C++11 introduced move semantics, variadic templates, initializer lists, constexpr, range-based loops, etc..
What are some of the major features of C++14?
C++14 introduced std::make_unique, return type deduction, decltype(auto), generic lambda expressions, etc..
What is a high level difference between static_cast and dynamic_cast?
static_cast is a compile-time check. dynamic_cast is a runtime check using RTTI (runtime type information).
What are the 5 value categories and their major attributes?
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.
What does “cv-qualified” mean?
Having the ‘const’ or ‘volatile’ type qualifiers.
What are “storage class specifiers”?
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).
What is a “function-local predefined variable”
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”;
What is in “class scope”?
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.
What is the difference between “function parameter scope” and “function scope”?
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.
What is namespace scope?
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.
What is the difference between a scoped and unscoped enumeration?
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.
What is a “function-try-block” and it’s main purpose?
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.
Why must class and struct definitions end with a semi-colon?
As an optional list of instances may be declared after the body, e.g. “struct foo {int a; int b;} x, y, z;”
What is the value category of the parameter ‘w’ in the following: void f(Widget&& w);
A parameter is always an lvalue, even if its type is an rvalue reference.
What do std::move and std::forward do at runtime?
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.
What is wrong with this code? ctor(const std::string text)
: value(std::move(text)) {…}
Param ‘text’ is declared as ‘const’, so it cannot be moved from. (It will compile, but copy construct ‘value’).
What is a “universal reference”?
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.
What is wrong with the following code?: void f(char name){/…*/}; f(“test”);
In C++, you cannot pass a string literal for a char *. The parameter would need to be “const char *”.
What produces an xvalue expression?
std::move, and the result of calling a function whose return type is an rvalue reference is an xvalue.