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. Returning unique_ptr and preventing move
Forum Updated to NodeBB v4.3 + New Features

Returning unique_ptr and preventing move

Scheduled Pinned Locked Moved Solved C++ Gurus
5 Posts 3 Posters 2.0k Views 4 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.
  • E Offline
    E Offline
    Exotic_Devel
    wrote on last edited by Exotic_Devel
    #1

    I have a class that has a property of type std::unique_ptr. I am implementing the gets and sets methods of this class and I need that the unique_ptr that is returned by the get method cannot be moved, ie that the pointer cannot be moved with std::move.
    How does this return have to be to guarantee this?

    class Operator
    {
    	private:
    		QString _name;
    		QString _login;
    		QString _passw;
    		std::unique_ptr<Profile> _profile;
    
    	public:
    		Operator();
    		Operator(const QString &name, const QString &login, const QString &passw,
    						 std::unique_ptr<Profile> getProfile);
    		QString getName() const;
    		void setName(const QString &name);
    		QString getLogin() const;
    		void setLogin(const QString &login);
    		QString getPassw() const;
    		void setPassw(const QString &passw);
    		std::unique_ptr<Profile> getProfile() const; //Should I return by reference, by value, by lvalue (&), rvalue (&&)?
    		void setProfile(std::unique_ptr<Profile> profile);
    };
    
    kshegunovK 1 Reply Last reply
    0
    • E Exotic_Devel

      I have a class that has a property of type std::unique_ptr. I am implementing the gets and sets methods of this class and I need that the unique_ptr that is returned by the get method cannot be moved, ie that the pointer cannot be moved with std::move.
      How does this return have to be to guarantee this?

      class Operator
      {
      	private:
      		QString _name;
      		QString _login;
      		QString _passw;
      		std::unique_ptr<Profile> _profile;
      
      	public:
      		Operator();
      		Operator(const QString &name, const QString &login, const QString &passw,
      						 std::unique_ptr<Profile> getProfile);
      		QString getName() const;
      		void setName(const QString &name);
      		QString getLogin() const;
      		void setLogin(const QString &login);
      		QString getPassw() const;
      		void setPassw(const QString &passw);
      		std::unique_ptr<Profile> getProfile() const; //Should I return by reference, by value, by lvalue (&), rvalue (&&)?
      		void setProfile(std::unique_ptr<Profile> profile);
      };
      
      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @Exotic_Devel said in Returning unique_ptr and preventing move:

      Profile * getProfile() const;
      

      Is simplest. The owned object continues to live where it does, you just return the member. If you can be sure that Profile is always valid, you can even return Profile &.

      Read and abide by the Qt Code of Conduct

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

        It looks like you can return smart pointers via RVO, but it might require C++17:
        https://www.internalpointers.com/post/move-smart-pointers-and-out-functions-modern-c

        The comments also mention using std::move(ptr) to return a smart pointer, but you seemed to indicate problems with that.

        C++ is a perfectly valid school of magic.

        kshegunovK 1 Reply Last reply
        0
        • fcarneyF fcarney

          It looks like you can return smart pointers via RVO, but it might require C++17:
          https://www.internalpointers.com/post/move-smart-pointers-and-out-functions-modern-c

          The comments also mention using std::move(ptr) to return a smart pointer, but you seemed to indicate problems with that.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @fcarney said in Returning unique_ptr and preventing move:

          The comments also mention using std::move(ptr) to return a smart pointer, but you seemed to indicate problems with that.

          The typical problem being that returning the unique_ptr transfers ownership.

          Read and abide by the Qt Code of Conduct

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

            @kshegunov said in Returning unique_ptr and preventing move:

            The typical problem being that returning the unique_ptr transfers ownership.

            I am confused then. I thought he had trouble doing this. I didnt understand that he didnt want it to move.

            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