Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. qAsConst() deprecated

qAsConst() deprecated

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 2.2k 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.
  • C Offline
    C Offline
    ChortleMortal
    wrote on last edited by ChortleMortal
    #1

    Seeing that qAsConst() is now deprecated, I have this question about preventing detachment.

    Given
    QList<Edge> edges;

    for (auto & edge : std::as_const(edges))
    {
        doSomething(edge);
    }
    
    for (const auto & edge : edges)
    {
        doSomething(edge);
    }
    
    

    Are these two ways doing exactly the same thing, or does the second way detach?

    Chris KawaC 1 Reply Last reply
    0
    • C ChortleMortal

      Seeing that qAsConst() is now deprecated, I have this question about preventing detachment.

      Given
      QList<Edge> edges;

      for (auto & edge : std::as_const(edges))
      {
          doSomething(edge);
      }
      
      for (const auto & edge : edges)
      {
          doSomething(edge);
      }
      
      

      Are these two ways doing exactly the same thing, or does the second way detach?

      Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      @ChortleMortal Second one detaches. It's equivalent to

      for (auto it = edges.begin(); it != edges.end(); ++it)
      {
         const auto& edge = *it;
         doSomething(edge);
      }
      

      It doesn't matter if edge is const or not. Detach happens on the call to begin() on non-const container, so before the assignment to the variable even takes place.

      C 1 Reply Last reply
      3
      • Chris KawaC Chris Kawa

        @ChortleMortal Second one detaches. It's equivalent to

        for (auto it = edges.begin(); it != edges.end(); ++it)
        {
           const auto& edge = *it;
           doSomething(edge);
        }
        

        It doesn't matter if edge is const or not. Detach happens on the call to begin() on non-const container, so before the assignment to the variable even takes place.

        C Offline
        C Offline
        ChortleMortal
        wrote on last edited by
        #3

        @Chris-Kawa
        Thanks so much Chris. You have saved me from making a systematic errror. Much appreciated.

        1 Reply Last reply
        0
        • C ChortleMortal has marked this topic as solved on
        • V Offline
          V Offline
          Volker75
          wrote on last edited by
          #4
          This post is deleted!
          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