Lifetime of lambda objects in relation to function pointer conversion

§5.1.2/6 says: The closure type for a lambda-expression with no lambda-capture has a public non-virtual non-explicit const conversion function to pointer to function having the same parameter and return types as the closure type’s function call operator. The value returned by this conversion function shall be the address of a function that, when invoked, has … Read more

call to pure virtual function from base class constructor

There are many articles that explain why you should never call virtual functions in constructor and destructor in C++. Take a look here and here for details what happens behind the scene during such calls. In short, objects are constructed from the base up to the derived. So when you try to call a virtual … Read more

Difference between IOptionsMonitor vs. IOptionsSnapshot

IOptionsMonitor is a singleton service that retrieves current option values at any time, which is especially useful in singleton dependencies. IOptionsSnapshot is a scoped service and provides a snapshot of the options at the time the IOptionsSnapshot<T> object is constructed. Options snapshots are designed for use with transient and scoped dependencies. Use IOptions<T> when you … Read more

Exact moment of “return” in a C++-function

Due to Return Value Optimization (RVO), a destructor for std::string res in make_string_ok may not be called. The string object can be constructed on the caller’s side and the function may only initialize the value. The code will be equivalent to: void make_string_ok(std::string& res){ Writer w(res); } int main() { std::string res(“A”); make_string_ok(res); } That … Read more

Why would the behavior of std::memcpy be undefined for objects that are not TriviallyCopyable?

Why would the behavior of std::memcpy itself be undefined when used with non-TriviallyCopyable objects? It’s not! However, once you copy the underlying bytes of one object of a non-trivially copyable type into another object of that type, the target object is not alive. We destroyed it by reusing its storage, and haven’t revitalized it by … Read more

Object destruction in C++

In the following text, I will distinguish between scoped objects, whose time of destruction is statically determined by their enclosing scope (functions, blocks, classes, expressions), and dynamic objects, whose exact time of destruction is generally not known until runtime. While the destruction semantics of class objects are determined by destructors, the destruction of a scalar … Read more