Tag: lifetime
-
Does a const char* literal string persistently exist as long as the process alive?
12 I have functions like the following: const char* get_message() { return "This is a constant message, will NOT change forever!"; }; const char* get_message2() { return "message2"; }; And I’m planning to use them everywhere my app, even though in different threads. I’m wondering about the life time of these strings, i.e. whether it’s…
-
Lambda passed by reference runs when invoked in the constructor, but not when later stored in a data member
27 The following C++ code prints 11.1 then crashes. The lambda function seems to be called correctly inside the constructor, but then later, that same function no longer works! Why is this? Does the lambda have a limited lifespan? #include <functional> #include <iostream> class LambdaStore { public: LambdaStore(const std::function<void(float)>& _fn) : fn(_fn) { fn(11.1f); //…
-
std::move and lifetime of temporary objects
11 Can someone explain the execution order of this code? struct Foo { ~Foo() { std::cout << "1"; } }; int main() { const Foo& bar = Foo(); const Foo& baz = std::move(Foo()); std::cout << "2"; } The following code prints 121. I understand why I get 1 after 2, it’s because the lifetime of…