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 ?

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.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by Qt embedded developer
    #1

    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 ?

    JoeCFDJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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
      4
      • SGaistS SGaist

        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 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.

        SGaistS 1 Reply Last reply
        0
        • Q Qt embedded developer

          @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.

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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

            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 ?

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on 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.

            kshegunovK Q 2 Replies Last reply
            0
            • JoeCFDJ JoeCFD

              @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.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on 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

              JoeCFDJ 1 Reply Last reply
              0
              • kshegunovK kshegunov

                @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());
                
                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on 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.

                kshegunovK S 2 Replies Last reply
                0
                • JoeCFDJ JoeCFD

                  @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.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on 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

                  JoeCFDJ 1 Reply Last reply
                  0
                  • JoeCFDJ JoeCFD

                    @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 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.

                    JoeCFDJ 1 Reply Last reply
                    0
                    • kshegunovK kshegunov

                      @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.

                      JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • S SamiV123

                        @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.

                        JoeCFDJ Offline
                        JoeCFDJ Offline
                        JoeCFD
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        • JoeCFDJ JoeCFD

                          @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 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 ?

                          JoeCFDJ 1 Reply Last reply
                          0
                          • Q Qt embedded developer has marked this topic as solved on
                          • Q Qt embedded developer

                            @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 ?

                            JoeCFDJ Offline
                            JoeCFDJ Offline
                            JoeCFD
                            wrote on 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
                            • Christian EhrlicherC Christian Ehrlicher referenced this topic on

                            • Login

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