Extending temporary’s lifetime through rvalue data-member works with aggregate, but not with constructor, why?

TL;DR Aggregate initialization can be used to extend the life-time of a temporary, a user-defined constructor cannot do the same since it’s effectively a function call. Note: Both T const& and T&& apply in the case of aggregate-initalization and extending the life of temporaries bound to them. What is an Aggregate? struct S { // … Read more

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

Is circumventing a class’ constructor legal or does it result in undefined behaviour?

It is legal now, and retroactively since C++98! Indeed the C++ specification wording till C++20 was defining an object as (e.g. C++17 wording, [intro.object]): The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is created by a definition (6.1), by a new-expression (8.5.2.4), when implicitly changing the active … Read more

When to use PerThreadLifetimeManager?

The Per Thread Lifetime is a very dangerous lifestyle and in general you should not use it in your application, especially web applications. This lifestyle should be considered dangerous, because it is very hard to predict what the actual lifespan of a thread is. When you create and start a thread using new Thread().Start(), you’ll … Read more

Why does calling std::string.c_str() on a function that returns a string not work?

getString() would return a copy of str (getString() returns by value); It’s right. thus, the copy of str would stay “alive” in main() until main() returns. No, the returned copy is a temporary std::string, which will be destroyed at the end of the statement in which it was created, i.e. before std::cout << cStr << … 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

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