What do braces on the left-hand side of a variable declaration mean, such as in T {x} = y?

It’s not a declaration. It’s an assignment to a temporary. In std::unique_ptr<int> {p} = std::make_unique<int>(1);, std::unique_ptr<int> {p} creates a unique_ptr temporary that takes ownership of the object p points to, then std::make_unique<int>(1) is assigned to that temporary, which causes the object p points to to be deleted and the temporary to take ownership of the … Read more

Is it safe to use the std::string::c_str() of a temporary string?

The pointer returned by std::string::c_str() points to memory maintained by the string object. It remains valid until a non-const function is called on the string object, or the string object is destructed. The string object you’re concerned about is a temporary. It will be destructed at the end of the full expression, not before and … Read more

What is the lifetime of temporary function arguments? [duplicate]

Temporary objects are destroyed at the end of the full expression they’re part of. A full expression is an expression that isn’t a sub-expression of some other expression. Usually this means it ends at the ; (or ) for if, while, switch etc.) denoting the end of the statement. In your example, it’s the end … Read more

How can the type of braces influence object lifetime in C++?

Gcc is correct. The lifetime of the temporary will be extended only when using list-initialization syntax (i.e. using braces) in initialization of an aggregate. (since C++20) a temporary bound to a reference in a reference element of an aggregate initialized using direct-initialization syntax (parentheses) as opposed to list-initialization syntax (braces) exists until the end of … Read more

Why doesn’t a const reference extend the life of a temporary object passed via a function?

It’s by design. In a nutshell, only the named reference to which the temporary is bound directly will extend its lifetime. [class.temporary] 5 There are three contexts in which temporaries are destroyed at a different point than the end of the full-expression. […] 6 The third context is when a reference is bound to a … Read more

Cv-qualifications of prvalues (revisited)

Why are you doubting the language of 7.2.2? This seems pretty unambiguous that cv qualifiers are discarded on non-class, non-array prvalues, so the type T in temporary materialization is a non-const, non-volatile type. If that weren’t the case, then you wouldn’t be able to bind prvalues to non-const rvalue references. Yet it seems overwhelmingly likely … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)