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. Copy/Move Constructor not invoked
Forum Updated to NodeBB v4.3 + New Features

Copy/Move Constructor not invoked

Scheduled Pinned Locked Moved Solved C++ Gurus
10 Posts 3 Posters 1.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.
  • V Offline
    V Offline
    Vinoth Rajendran4
    wrote on last edited by Vinoth Rajendran4
    #1

    Hi All,

    Can some one help me understand, why either copy/move constructor is not called in this scenario.

    class Hello
    {
    public:
        Hello() = default;
        Hello(const Hello&)
        {
            cout << "Copy ctor";
        }
    
        Hello(Hello&&)
       { 
           cout << "Move ctor";
       }
    
    };
    
    int main()
    {
        Hello a = Hello();
        return 0;
    }
    
    

    Does compiler does any optimization is this scenario ?

    JonBJ 1 Reply Last reply
    1
    • V Vinoth Rajendran4

      Hi All,

      Can some one help me understand, why either copy/move constructor is not called in this scenario.

      class Hello
      {
      public:
          Hello() = default;
          Hello(const Hello&)
          {
              cout << "Copy ctor";
          }
      
          Hello(Hello&&)
         { 
             cout << "Move ctor";
         }
      
      };
      
      int main()
      {
          Hello a = Hello();
          return 0;
      }
      
      

      Does compiler does any optimization is this scenario ?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Vinoth-Rajendran4
      I'm going to stick my neck, because I'm not a C++ expert like some the people here.

      But if you were expecting it to create a default for Hello a and then assign (copy/move) to it from = Hello(), I don't think it does that. I think C++ says this initialize-and-assign just create and initalizes with a Hello().

      V 1 Reply Last reply
      2
      • JonBJ JonB

        @Vinoth-Rajendran4
        I'm going to stick my neck, because I'm not a C++ expert like some the people here.

        But if you were expecting it to create a default for Hello a and then assign (copy/move) to it from = Hello(), I don't think it does that. I think C++ says this initialize-and-assign just create and initalizes with a Hello().

        V Offline
        V Offline
        Vinoth Rajendran4
        wrote on last edited by
        #3

        @JonB , I was expecting Hello() will call default constructor, followed by move constructor for object a creation.

        JonBJ 1 Reply Last reply
        0
        • V Vinoth Rajendran4

          @JonB , I was expecting Hello() will call default constructor, followed by move constructor for object a creation.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Vinoth-Rajendran4
          Yes. and I'm saying I believe C++ says create-and-assign is not treated as create followed by assign, it's just one operation. But I don't have a reference for you, so wait till someone other says so...! :)

          To allay your worries about optimization, I would do something with a just to make sure.

          V kshegunovK 2 Replies Last reply
          0
          • JonBJ JonB

            @Vinoth-Rajendran4
            Yes. and I'm saying I believe C++ says create-and-assign is not treated as create followed by assign, it's just one operation. But I don't have a reference for you, so wait till someone other says so...! :)

            To allay your worries about optimization, I would do something with a just to make sure.

            V Offline
            V Offline
            Vinoth Rajendran4
            wrote on last edited by
            #5

            @JonB . Thanks for your input.

            1 Reply Last reply
            0
            • JonBJ JonB

              @Vinoth-Rajendran4
              Yes. and I'm saying I believe C++ says create-and-assign is not treated as create followed by assign, it's just one operation. But I don't have a reference for you, so wait till someone other says so...! :)

              To allay your worries about optimization, I would do something with a just to make sure.

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

              @JonB said in Copy/Move Constructor not invoked:

              But I don't have a reference for you, so wait till someone other says so...!

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

              • If T is a class type,
                • if the initializer is a prvalue expression whose type is the same class as T (ignoring cv-qualification), the initializer expression itself, rather than a temporary materialized from it, is used to initialize the destination object.
                  (Before C++17, the compiler may elide the construction from the prvalue temporary in this case, but the appropriate constructor must still be accessible: see copy elision)

              Read and abide by the Qt Code of Conduct

              JonBJ 2 Replies Last reply
              4
              • kshegunovK kshegunov

                @JonB said in Copy/Move Constructor not invoked:

                But I don't have a reference for you, so wait till someone other says so...!

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

                • If T is a class type,
                  • if the initializer is a prvalue expression whose type is the same class as T (ignoring cv-qualification), the initializer expression itself, rather than a temporary materialized from it, is used to initialize the destination object.
                    (Before C++17, the compiler may elide the construction from the prvalue temporary in this case, but the appropriate constructor must still be accessible: see copy elision)
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @kshegunov
                Thank you!

                And, IIRC some issue over this, which C++ version says T t = T(); does the same as T t(T()); ? Have they always does behaved the same?

                kshegunovK 1 Reply Last reply
                0
                • JonBJ JonB

                  @kshegunov
                  Thank you!

                  And, IIRC some issue over this, which C++ version says T t = T(); does the same as T t(T()); ? Have they always does behaved the same?

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

                  @JonB said in Copy/Move Constructor not invoked:

                  And, IIRC some issue over this, which C++ version says T t = T(); does the same as T t(T()); ?

                  c++17

                  Have they always does behaved the same?

                  Yes for a very long time. Before codified into the standards the compilers just used to elide the copy (which they're free to do).

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  1
                  • kshegunovK kshegunov

                    @JonB said in Copy/Move Constructor not invoked:

                    But I don't have a reference for you, so wait till someone other says so...!

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

                    • If T is a class type,
                      • if the initializer is a prvalue expression whose type is the same class as T (ignoring cv-qualification), the initializer expression itself, rather than a temporary materialized from it, is used to initialize the destination object.
                        (Before C++17, the compiler may elide the construction from the prvalue temporary in this case, but the appropriate constructor must still be accessible: see copy elision)
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @kshegunov said in Copy/Move Constructor not invoked:

                    Before C++17, the compiler may elide the construction from the prvalue temporary in this case [...]

                    I'm still not understanding what this is saying. What is it that <= C++17 is allowed to do which >=C++20 is not?

                    kshegunovK 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @kshegunov said in Copy/Move Constructor not invoked:

                      Before C++17, the compiler may elide the construction from the prvalue temporary in this case [...]

                      I'm still not understanding what this is saying. What is it that <= C++17 is allowed to do which >=C++20 is not?

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

                      It says that before c++17 the compiler may direct initialize from the/a temporary but is not mandated to (although they used to do it). After c++17 it's not optional anymore.

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      5

                      • Login

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