Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. What is the lifetime of this temporary?
Forum Updated to NodeBB v4.3 + New Features

What is the lifetime of this temporary?

Scheduled Pinned Locked Moved Solved C++ Gurus
3 Posts 2 Posters 439 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #1
    socket->sendBinaryMessage(QByteArray(msgpack_buf.data(), msgpack_buf.size()));
    

    My gut says create the byte array before the call. My curiosity says let the world burn.
    sendBinaryMessage is wanting a reference, btw. This is a websockets call.

    C++ is a perfectly valid school of magic.

    jeremy_kJ 1 Reply Last reply
    0
    • fcarneyF fcarney
      socket->sendBinaryMessage(QByteArray(msgpack_buf.data(), msgpack_buf.size()));
      

      My gut says create the byte array before the call. My curiosity says let the world burn.
      sendBinaryMessage is wanting a reference, btw. This is a websockets call.

      jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by jeremy_k
      #2

      https://en.cppreference.com/w/cpp/language/lifetime

      Temporary objects are created when a prvalue is materialized so that it can be used as a glvalue, which occurs (since C++17) in the following situations:

      * binding a reference to a prvalue 
      

      [...]

      All temporary objects are destroyed as the last step in evaluating the full-expression that (lexically) contains the point where they were created, and if multiple temporary objects were created, they are destroyed in the order opposite to the order of creation. This is true even if that evaluation ends in throwing an exception.

      There are two exceptions from that:

      * The lifetime of a temporary object may be extended by binding to a const lvalue reference or to an rvalue reference (since C++11), see reference initialization for details.
      * The lifetime of a temporary object created when evaluating the default arguments of a default or copy constructor used to initialize an element of an array ends before the next element of the array begins initialization. 
      

      The LaTeX source for the relevant standard section is at https://github.com/cplusplus/draft/blob/master/source/basic.tex

      The QByteArray will be created prior to binding it to the reference passed to sendBinaryMessage, and destroyed when the function returns.

      One technique for experimentally exploring this type of problem is to add print statements to constructors, destructors, and operater= overloads to clarify when and how an object is accessed.

      eg:

      class MyByteArray: public QByteArray {
          MyByteArray(...) : QByteArray(...) { printf("MyByteArray()\n"); }
      };
      

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      3
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #3

        This is interesting:

            // if argument is a const then lifetime of temporary is extended
            auto func = [](const QByteArray& str){
                qDebug() << str;
            };
        
            func(QByteArray("hello"));
            QByteArray str2("world");
            func(str2);
        

        @jeremy_k , Like the references you provided say it will not compile unless that function is a const. This makes sense to me the way this behaves.

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved