Category : temporary-objects

Currently I’m doing some code reviews and stumbled on the following construct: QVariantMap argumentMap = QJsonDocument::fromJson(" … JSON-String … ", &error).toVariant().toMap(); … QListIterator<QVariant> keyIterator( argumentMap["key"].toList() ); while ( keyIterator.hasNext() ) … My first feeling was that the iterator is faulty here, as toList() returns a QVariantList by value resulting in a temporary object. So the ..

Read more

From [class.temporary] of the Working Draft, Standard for Programming Language C++: (6.12) — A temporary bound to a reference in a new-initializer ([expr.new]) persists until the completion of the full-expression containing the new-initializer. [Note 7: This might introduce a dangling reference. — end note] [Example 5: struct S { int mi; const std::pair<int,int>& mp; }; ..

Read more

C-Code #include <stdio.h> typedef struct { int a; }A; int main() { A(); // this line gives error return 0; } Output Error: Expected identifier or ‘(‘ C++-Code #include <iostream> struct A { int a; A() { std::cout<<"Ctor-An"; } ~A() { std::cout<<"Dctor-An"; } }; int main() { A(); // creates temporary object and destroyed it ..

Read more

This question is a follow-up question to: Second overload of std::foward (example on cppreference.com). StoryTeller’s answer made me think about the value categories involved in the statement foo(forward<decltype(forward<T>(arg).get())>(forward<T>(arg).get()));. Doesn’t the second overload template< class T > constexpr T&& forward( std::remove_reference_t<T>&& t ) noexcept; potentially cause dangling references? Changing my example of the linked question to ..

Read more