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 difference between std::unique_ptr V/s std::make_unique ?
QtWS25 Last Chance

what is difference between std::unique_ptr V/s std::make_unique ?

Scheduled Pinned Locked Moved Solved C++ Gurus
13 Posts 5 Posters 3.2k Views
  • 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.
  • S Offline
    S Offline
    SGaist
    Lifetime Qt Champion
    wrote on 24 Feb 2024, 08:35 last edited by
    #2

    Hi,

    Unlike new, make_unique is exception safe.
    No cleanup is necessary if make_unique is not evaluated.

    As for your code: why are you creating a unique pointer out of a stack based variable ?

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    Q 1 Reply Last reply 24 Feb 2024, 08:58
    4
    • S SGaist
      24 Feb 2024, 08:35

      Hi,

      Unlike new, make_unique is exception safe.
      No cleanup is necessary if make_unique is not evaluated.

      As for your code: why are you creating a unique pointer out of a stack based variable ?

      Q Offline
      Q Offline
      Qt embedded developer
      wrote on 24 Feb 2024, 08:58 last edited by Qt embedded developer
      #3

      @SGaist Becuase the scope is within that main() so i have used that way.

      In response to your question " My ClassName is only and only allowed to to call in main(). and not out side of it. And also I don't want any type of pointer error or freeing it manually so. i used it."

      But i want to know all my question answer one by one. So if you give the explanation then it will help me next time what to use and what not to use.

      S 1 Reply Last reply 24 Feb 2024, 10:20
      0
      • Q Qt embedded developer
        24 Feb 2024, 08:58

        @SGaist Becuase the scope is within that main() so i have used that way.

        In response to your question " My ClassName is only and only allowed to to call in main(). and not out side of it. And also I don't want any type of pointer error or freeing it manually so. i used it."

        But i want to know all my question answer one by one. So if you give the explanation then it will help me next time what to use and what not to use.

        S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 24 Feb 2024, 10:20 last edited by
        #4

        @Qt-embedded-developer the answer is basically: don't use 1 for the reasons given in my answer.

        That said, you should really learn the use cases that comes with it. Using unique pointers is not a silver bullet. Especially in the case of Qt where there's the parent-child relationship paradigm.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • Q Qt embedded developer
          24 Feb 2024, 08:10

          I want to know when to use line 1 and when to use line 2 ? and what is difference between std::unique_ptr V/s std::make_unique ?

          Line 1:

          • std::unique_ptr<ClassName> classNamePtr = std::unique_ptr<MainApplication>(new MainApplication(&app));

          Line 2:

          • std::unique_ptr<ClassName> classNamePtr = std::make_unique<ClassName>(&app);

          Can Anybody explain what is meaning of this both line ?

          If i want to use QT shared pointer then what change is needed in above line ?

          What is advantage of it ?

          J Offline
          J Offline
          JoeCFD
          wrote on 24 Feb 2024, 20:32 last edited by JoeCFD
          #5

          @Qt-embedded-developer said in what is difference between std::unique_ptr V/s std::make_unique ?:

          std::make_unique

          I guess there is no difference between line1 and line2.
          std::make_shared was introduced in C++11, but std::make_unique was not.
          If you use C++ 11, you can use only line 1, but not Line2.
          Line2 was introduced in C++ 14. Therefore, from C++14, Line1 and Line2 are same.
          Line2 is preferred since it is consistent with make_shared.

          As @SGaist pointed out, auto classNamePtr = std::make_unique<ClassName>(&app); can be fatal.

          K Q 2 Replies Last reply 24 Feb 2024, 23:43
          0
          • J JoeCFD
            24 Feb 2024, 20:32

            @Qt-embedded-developer said in what is difference between std::unique_ptr V/s std::make_unique ?:

            std::make_unique

            I guess there is no difference between line1 and line2.
            std::make_shared was introduced in C++11, but std::make_unique was not.
            If you use C++ 11, you can use only line 1, but not Line2.
            Line2 was introduced in C++ 14. Therefore, from C++14, Line1 and Line2 are same.
            Line2 is preferred since it is consistent with make_shared.

            As @SGaist pointed out, auto classNamePtr = std::make_unique<ClassName>(&app); can be fatal.

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 24 Feb 2024, 23:43 last edited by
            #6

            @JoeCFD said in what is difference between std::unique_ptr V/s std::make_unique ?:

            Line2 was introduced in C++ 14. Therefore, from C++14, Line1 and Line2 are same.

            In this example yes, but not in general. Consider this:

            class A;
            int g() { throw 0; }
            void f(std::unique_ptr<A> a, int p);
            

            which can be used safely like this:

            f(std::make_unique<A>(...), g());
            

            while this may leak depending on how the compiler evaluates the arguments (which is unspecified):

            f(std::unique_ptr<A>(new A()), g());
            

            Read and abide by the Qt Code of Conduct

            J 1 Reply Last reply 25 Feb 2024, 01:41
            0
            • K kshegunov
              24 Feb 2024, 23:43

              @JoeCFD said in what is difference between std::unique_ptr V/s std::make_unique ?:

              Line2 was introduced in C++ 14. Therefore, from C++14, Line1 and Line2 are same.

              In this example yes, but not in general. Consider this:

              class A;
              int g() { throw 0; }
              void f(std::unique_ptr<A> a, int p);
              

              which can be used safely like this:

              f(std::make_unique<A>(...), g());
              

              while this may leak depending on how the compiler evaluates the arguments (which is unspecified):

              f(std::unique_ptr<A>(new A()), g());
              
              J Offline
              J Offline
              JoeCFD
              wrote on 25 Feb 2024, 01:41 last edited by JoeCFD
              #7

              @kshegunov I normally avoid passing any unique pointer in a func. If I have to, I prefer to define it as a shared pointer since it does not cost anything and why I look for trouble.

              K S 2 Replies Last reply 25 Feb 2024, 10:48
              0
              • J JoeCFD
                25 Feb 2024, 01:41

                @kshegunov I normally avoid passing any unique pointer in a func. If I have to, I prefer to define it as a shared pointer since it does not cost anything and why I look for trouble.

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 25 Feb 2024, 10:48 last edited by
                #8

                @JoeCFD said in what is difference between std::unique_ptr V/s std::make_unique ?:

                If I have to, I prefer to define it as a shared pointer since it does not cost anything and why I look for trouble.

                That's up to you, but keeping everything in std::shared_ptr, just because it's easy (for some definition of easy), is a rather lazy way of programming. There is a significant semantic difference between the two.

                Read and abide by the Qt Code of Conduct

                J 1 Reply Last reply 26 Feb 2024, 03:50
                0
                • J JoeCFD
                  25 Feb 2024, 01:41

                  @kshegunov I normally avoid passing any unique pointer in a func. If I have to, I prefer to define it as a shared pointer since it does not cost anything and why I look for trouble.

                  S Offline
                  S Offline
                  SamiV123
                  wrote on 25 Feb 2024, 17:34 last edited by
                  #9

                  @JoeCFD

                  you pass unique_ptr to a function when you want to transfer the ownership of the object contained inside the unique_ptr to the callee.

                  Using a shared_ptr is rarely the right choice.

                  If you don't transfer the ownership use a raw ptr or reference.

                  J 1 Reply Last reply 26 Feb 2024, 03:52
                  0
                  • K kshegunov
                    25 Feb 2024, 10:48

                    @JoeCFD said in what is difference between std::unique_ptr V/s std::make_unique ?:

                    If I have to, I prefer to define it as a shared pointer since it does not cost anything and why I look for trouble.

                    That's up to you, but keeping everything in std::shared_ptr, just because it's easy (for some definition of easy), is a rather lazy way of programming. There is a significant semantic difference between the two.

                    J Offline
                    J Offline
                    JoeCFD
                    wrote on 26 Feb 2024, 03:50 last edited by
                    #10

                    @kshegunov I am lazy indeed now. Will study more on std::unique_ptr

                    1 Reply Last reply
                    0
                    • S SamiV123
                      25 Feb 2024, 17:34

                      @JoeCFD

                      you pass unique_ptr to a function when you want to transfer the ownership of the object contained inside the unique_ptr to the callee.

                      Using a shared_ptr is rarely the right choice.

                      If you don't transfer the ownership use a raw ptr or reference.

                      J Offline
                      J Offline
                      JoeCFD
                      wrote on 26 Feb 2024, 03:52 last edited by
                      #11

                      @SamiV123 I see your point and will try to find some use cases.

                      1 Reply Last reply
                      0
                      • J JoeCFD
                        24 Feb 2024, 20:32

                        @Qt-embedded-developer said in what is difference between std::unique_ptr V/s std::make_unique ?:

                        std::make_unique

                        I guess there is no difference between line1 and line2.
                        std::make_shared was introduced in C++11, but std::make_unique was not.
                        If you use C++ 11, you can use only line 1, but not Line2.
                        Line2 was introduced in C++ 14. Therefore, from C++14, Line1 and Line2 are same.
                        Line2 is preferred since it is consistent with make_shared.

                        As @SGaist pointed out, auto classNamePtr = std::make_unique<ClassName>(&app); can be fatal.

                        Q Offline
                        Q Offline
                        Qt embedded developer
                        wrote on 26 Feb 2024, 08:50 last edited by
                        #12

                        @JoeCFD said in what is difference between std::unique_ptr V/s std::make_unique ?:

                        As @SGaist pointed out, auto classNamePtr = std::make_unique<ClassName>(&app); can be fatal.

                        Can you elaborate this thing because why it can be fatal ?

                        J 1 Reply Last reply 26 Feb 2024, 14:09
                        0
                        • Q Qt embedded developer has marked this topic as solved on 26 Feb 2024, 11:18
                        • Q Qt embedded developer
                          26 Feb 2024, 08:50

                          @JoeCFD said in what is difference between std::unique_ptr V/s std::make_unique ?:

                          As @SGaist pointed out, auto classNamePtr = std::make_unique<ClassName>(&app); can be fatal.

                          Can you elaborate this thing because why it can be fatal ?

                          J Offline
                          J Offline
                          JoeCFD
                          wrote on 26 Feb 2024, 14:09 last edited by
                          #13

                          @Qt-embedded-developer app looks like using stack(not heap) memory. when classNamePtr is cleared, it will try to destroy &app and your app will crash likely.

                          1 Reply Last reply
                          0
                          • C Christian Ehrlicher referenced this topic on 28 Feb 2024, 14:36

                          11/13

                          26 Feb 2024, 03:52

                          • Login

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