Why is the copy constructor called instead of the move constructor when returning?When should std::move be used on a function return value?Why don't C++ compilers define operator== and operator!=?Can I call a constructor from another constructor (do constructor chaining) in C++?When should I really use noexcept?Why is my object not being copied when move constructor is deleted?Is the default Move constructor defined as noexcept?Why is the copy constructor called instead of the move constructor?When does returning a value outside of a function uses move vs copy?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationscopy constructor called instead of move constructor, why?Why is this deleted move constructor accepted by the compiler?

How did Einstein know the speed of light was constant?

How did שְׁלֹמֹה (shlomo) become Solomon?

How can solar sailed ships be protected from space debris?

Does Evolution Sage proliferate Blast Zone when played?

Minimizing medical costs with HSA

I turned off BitLocker encryption on my 5TB drive and now it continues to decrypt for days!

How can I effectively map a multi-level dungeon?

My players like to search everything. What do they find?

How can power levels matter in a magic system that emphasizes control?

Is it possible to spoof an IP address to an exact number?

Do the 26 richest billionaires own as much wealth as the poorest 3.8 billion people?

Term for a character that only exists to be talked to

How do both sides know the MTU

Show that there are infinitely more problems than we will ever be able to compute

Contributing to a candidate as a Foreign National US Resident?

What/Where usage English vs Japanese

Phrasing "it says" or "it reads"

Can 4 Joy cons connect to the same Switch?

Should I warn my boss I might take sick leave

Why does the Batman "crack his knuckles" in "Batman: Arkham Origins"?

How should I present a resort brochure in my general fiction?

3D nonogram – What's in the box?

Was Wolfgang Unzicker the last Amateur GM?

Using Sed to add counter to keyword



Why is the copy constructor called instead of the move constructor when returning?


When should std::move be used on a function return value?Why don't C++ compilers define operator== and operator!=?Can I call a constructor from another constructor (do constructor chaining) in C++?When should I really use noexcept?Why is my object not being copied when move constructor is deleted?Is the default Move constructor defined as noexcept?Why is the copy constructor called instead of the move constructor?When does returning a value outside of a function uses move vs copy?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationscopy constructor called instead of move constructor, why?Why is this deleted move constructor accepted by the compiler?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








8















Let’s say I have the class MyClass with a correct move constructor and whose copy constructor is deleted. Now I am returning this class like this:



MyClass func()

return MyClass();



In this case the move constructor gets called when returning the class object and everything works as expected.



Now let’s say MyClass has an implementation of the << operator:



MyClass& operator<<(MyClass& target, const int& source)

target.add(source);
return target;



When I change the code above:



MyClass func()

return MyClass() << 5;



I get the compiler error, that the copy constructor cannot be accessed because it is deleted. But why is the copy constructor being used at all in this case?










share|improve this question






























    8















    Let’s say I have the class MyClass with a correct move constructor and whose copy constructor is deleted. Now I am returning this class like this:



    MyClass func()

    return MyClass();



    In this case the move constructor gets called when returning the class object and everything works as expected.



    Now let’s say MyClass has an implementation of the << operator:



    MyClass& operator<<(MyClass& target, const int& source)

    target.add(source);
    return target;



    When I change the code above:



    MyClass func()

    return MyClass() << 5;



    I get the compiler error, that the copy constructor cannot be accessed because it is deleted. But why is the copy constructor being used at all in this case?










    share|improve this question


























      8












      8








      8


      3






      Let’s say I have the class MyClass with a correct move constructor and whose copy constructor is deleted. Now I am returning this class like this:



      MyClass func()

      return MyClass();



      In this case the move constructor gets called when returning the class object and everything works as expected.



      Now let’s say MyClass has an implementation of the << operator:



      MyClass& operator<<(MyClass& target, const int& source)

      target.add(source);
      return target;



      When I change the code above:



      MyClass func()

      return MyClass() << 5;



      I get the compiler error, that the copy constructor cannot be accessed because it is deleted. But why is the copy constructor being used at all in this case?










      share|improve this question
















      Let’s say I have the class MyClass with a correct move constructor and whose copy constructor is deleted. Now I am returning this class like this:



      MyClass func()

      return MyClass();



      In this case the move constructor gets called when returning the class object and everything works as expected.



      Now let’s say MyClass has an implementation of the << operator:



      MyClass& operator<<(MyClass& target, const int& source)

      target.add(source);
      return target;



      When I change the code above:



      MyClass func()

      return MyClass() << 5;



      I get the compiler error, that the copy constructor cannot be accessed because it is deleted. But why is the copy constructor being used at all in this case?







      c++ c++11 return copy move






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 25 at 21:45









      Peter Mortensen

      14.2k19 gold badges88 silver badges115 bronze badges




      14.2k19 gold badges88 silver badges115 bronze badges










      asked Jun 25 at 10:02









      RomCooRomCoo

      1,6082 gold badges16 silver badges27 bronze badges




      1,6082 gold badges16 silver badges27 bronze badges






















          3 Answers
          3






          active

          oldest

          votes


















          14















          Now I am returning this class via lvalue like this:



          MyClass func()

          return MyClass();




          No, the returned expression is an xvalue (a kind of rvalue), used to initialise the result for return-by-value (things are a little more complicated since C++17, but this is still the gist of it; besides, you're on C++11).




          In this case the move constructor gets called when returning the class object and everything works as expected.




          Indeed; an rvalue will initialise an rvalue reference and thus the whole thing can match move constructors.




          When I change the code above:




          … now the expression is MyClass() << 5, which has type MyClass&. This is never an rvalue. It's an lvalue. It's an expression that refers to an existing object.



          So, without an explicit std::move, that'll be used to copy-initialise the result. And, since your copy constructor is deleted, that can't work.




          I'm surprised the example compiles at all, since a temporary can't be used to initialise an lvalue reference (your operator's first argument), though some toolchains (MSVS) are known to accept this as an extension.





          then would return std::move(MyClass() << 5); work?




          Yes, I believe so.



          However that is very strange to look at, and makes the reader double-check to ensure there are no dangling references. This suggests there's a better way to accomplish this that results in clearer code:



          MyClass func()

          MyClass m;
          m << 5;
          return m;



          Now you're still getting a move (because that's a special rule when returning local variables) without any strange antics. And, as a bonus, the << call is completely standard-compliant.






          share|improve this answer

























          • then would return std::move(MyClass() << 5); work?

            – RomCoo
            Jun 25 at 10:22











          • @RomCoo I believe so. However that is very strange to look at and suggests there's a better way to accomplish this.

            – Lightness Races in Orbit
            Jun 25 at 10:23












          • Is the expression MyClass() << 5 valid? I don't think so! gcc and clang properly reject this expression. mvc accepts it ... You cannot bind rvalue to noconst lvalue

            – Biagio Festa
            Jun 25 at 11:21







          • 2





            @BiagioFesta Yes I said that in my answer

            – Lightness Races in Orbit
            Jun 25 at 11:31


















          7














          Your operator return by MyClass&. So you are returning an lvalue, not an rvalue that can be moved automatically.



          You can avoid the copy by relying on the standard guarantees regarding NRVO.



          MyClass func()

          MyClass m;
          m << 5;
          return m;



          This will either elide the object entirely, or move it. All on account of it being a function local object.




          Another option, seeing as you are trying to call operator<< on an rvalue, is to supply an overload dealing in rvalue references.



          MyClass&& operator<<(MyClass&& target, int i) 
          target << i; // Reuse the operator you have, here target is an lvalue
          return std::move(target);



          That will make MyClass() << 5 itself well formed (see the other answer for why it isn't), and return an xvalue from which the return object may be constructed. Though such and overload for operator<< is not commonly seen.






          share|improve this answer

























          • Presumably he wanted to return the result of operator<<, not m.

            – David Schwartz
            Jun 25 at 10:06











          • @DavidSchwartz - One can also presume operator<< is idiomatic and ends in return target; The signature would suggest so.

            – StoryTeller
            Jun 25 at 10:08











          • But the lack of a const on the first parameter, at least to me, suggests otherwise. Also, that would really not be idiomatic. It'd be odd if i<<3 returned i.

            – David Schwartz
            Jun 25 at 10:08












          • @DavidSchwartz - That's quite a leap either way. I think we both hastened to answer instead of asking the OP for a better example.

            – StoryTeller
            Jun 25 at 10:09






          • 1





            @DavidSchwartz - Not idiomatic, what? That's what stream operators do.

            – StoryTeller
            Jun 25 at 10:09


















          1














          Your operator<< takes its first parameter as a non-const reference. You can't bind a non-const reference to a temporary. But MyClass() returns the newly-created instance as a temporary.



          Also, while func returns a value, operator<< returns a reference. So what else can it do but make a copy to return?






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56751481%2fwhy-is-the-copy-constructor-called-instead-of-the-move-constructor-when-returnin%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            14















            Now I am returning this class via lvalue like this:



            MyClass func()

            return MyClass();




            No, the returned expression is an xvalue (a kind of rvalue), used to initialise the result for return-by-value (things are a little more complicated since C++17, but this is still the gist of it; besides, you're on C++11).




            In this case the move constructor gets called when returning the class object and everything works as expected.




            Indeed; an rvalue will initialise an rvalue reference and thus the whole thing can match move constructors.




            When I change the code above:




            … now the expression is MyClass() << 5, which has type MyClass&. This is never an rvalue. It's an lvalue. It's an expression that refers to an existing object.



            So, without an explicit std::move, that'll be used to copy-initialise the result. And, since your copy constructor is deleted, that can't work.




            I'm surprised the example compiles at all, since a temporary can't be used to initialise an lvalue reference (your operator's first argument), though some toolchains (MSVS) are known to accept this as an extension.





            then would return std::move(MyClass() << 5); work?




            Yes, I believe so.



            However that is very strange to look at, and makes the reader double-check to ensure there are no dangling references. This suggests there's a better way to accomplish this that results in clearer code:



            MyClass func()

            MyClass m;
            m << 5;
            return m;



            Now you're still getting a move (because that's a special rule when returning local variables) without any strange antics. And, as a bonus, the << call is completely standard-compliant.






            share|improve this answer

























            • then would return std::move(MyClass() << 5); work?

              – RomCoo
              Jun 25 at 10:22











            • @RomCoo I believe so. However that is very strange to look at and suggests there's a better way to accomplish this.

              – Lightness Races in Orbit
              Jun 25 at 10:23












            • Is the expression MyClass() << 5 valid? I don't think so! gcc and clang properly reject this expression. mvc accepts it ... You cannot bind rvalue to noconst lvalue

              – Biagio Festa
              Jun 25 at 11:21







            • 2





              @BiagioFesta Yes I said that in my answer

              – Lightness Races in Orbit
              Jun 25 at 11:31















            14















            Now I am returning this class via lvalue like this:



            MyClass func()

            return MyClass();




            No, the returned expression is an xvalue (a kind of rvalue), used to initialise the result for return-by-value (things are a little more complicated since C++17, but this is still the gist of it; besides, you're on C++11).




            In this case the move constructor gets called when returning the class object and everything works as expected.




            Indeed; an rvalue will initialise an rvalue reference and thus the whole thing can match move constructors.




            When I change the code above:




            … now the expression is MyClass() << 5, which has type MyClass&. This is never an rvalue. It's an lvalue. It's an expression that refers to an existing object.



            So, without an explicit std::move, that'll be used to copy-initialise the result. And, since your copy constructor is deleted, that can't work.




            I'm surprised the example compiles at all, since a temporary can't be used to initialise an lvalue reference (your operator's first argument), though some toolchains (MSVS) are known to accept this as an extension.





            then would return std::move(MyClass() << 5); work?




            Yes, I believe so.



            However that is very strange to look at, and makes the reader double-check to ensure there are no dangling references. This suggests there's a better way to accomplish this that results in clearer code:



            MyClass func()

            MyClass m;
            m << 5;
            return m;



            Now you're still getting a move (because that's a special rule when returning local variables) without any strange antics. And, as a bonus, the << call is completely standard-compliant.






            share|improve this answer

























            • then would return std::move(MyClass() << 5); work?

              – RomCoo
              Jun 25 at 10:22











            • @RomCoo I believe so. However that is very strange to look at and suggests there's a better way to accomplish this.

              – Lightness Races in Orbit
              Jun 25 at 10:23












            • Is the expression MyClass() << 5 valid? I don't think so! gcc and clang properly reject this expression. mvc accepts it ... You cannot bind rvalue to noconst lvalue

              – Biagio Festa
              Jun 25 at 11:21







            • 2





              @BiagioFesta Yes I said that in my answer

              – Lightness Races in Orbit
              Jun 25 at 11:31













            14












            14








            14








            Now I am returning this class via lvalue like this:



            MyClass func()

            return MyClass();




            No, the returned expression is an xvalue (a kind of rvalue), used to initialise the result for return-by-value (things are a little more complicated since C++17, but this is still the gist of it; besides, you're on C++11).




            In this case the move constructor gets called when returning the class object and everything works as expected.




            Indeed; an rvalue will initialise an rvalue reference and thus the whole thing can match move constructors.




            When I change the code above:




            … now the expression is MyClass() << 5, which has type MyClass&. This is never an rvalue. It's an lvalue. It's an expression that refers to an existing object.



            So, without an explicit std::move, that'll be used to copy-initialise the result. And, since your copy constructor is deleted, that can't work.




            I'm surprised the example compiles at all, since a temporary can't be used to initialise an lvalue reference (your operator's first argument), though some toolchains (MSVS) are known to accept this as an extension.





            then would return std::move(MyClass() << 5); work?




            Yes, I believe so.



            However that is very strange to look at, and makes the reader double-check to ensure there are no dangling references. This suggests there's a better way to accomplish this that results in clearer code:



            MyClass func()

            MyClass m;
            m << 5;
            return m;



            Now you're still getting a move (because that's a special rule when returning local variables) without any strange antics. And, as a bonus, the << call is completely standard-compliant.






            share|improve this answer
















            Now I am returning this class via lvalue like this:



            MyClass func()

            return MyClass();




            No, the returned expression is an xvalue (a kind of rvalue), used to initialise the result for return-by-value (things are a little more complicated since C++17, but this is still the gist of it; besides, you're on C++11).




            In this case the move constructor gets called when returning the class object and everything works as expected.




            Indeed; an rvalue will initialise an rvalue reference and thus the whole thing can match move constructors.




            When I change the code above:




            … now the expression is MyClass() << 5, which has type MyClass&. This is never an rvalue. It's an lvalue. It's an expression that refers to an existing object.



            So, without an explicit std::move, that'll be used to copy-initialise the result. And, since your copy constructor is deleted, that can't work.




            I'm surprised the example compiles at all, since a temporary can't be used to initialise an lvalue reference (your operator's first argument), though some toolchains (MSVS) are known to accept this as an extension.





            then would return std::move(MyClass() << 5); work?




            Yes, I believe so.



            However that is very strange to look at, and makes the reader double-check to ensure there are no dangling references. This suggests there's a better way to accomplish this that results in clearer code:



            MyClass func()

            MyClass m;
            m << 5;
            return m;



            Now you're still getting a move (because that's a special rule when returning local variables) without any strange antics. And, as a bonus, the << call is completely standard-compliant.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jun 25 at 10:24

























            answered Jun 25 at 10:19









            Lightness Races in OrbitLightness Races in Orbit

            306k57 gold badges505 silver badges850 bronze badges




            306k57 gold badges505 silver badges850 bronze badges












            • then would return std::move(MyClass() << 5); work?

              – RomCoo
              Jun 25 at 10:22











            • @RomCoo I believe so. However that is very strange to look at and suggests there's a better way to accomplish this.

              – Lightness Races in Orbit
              Jun 25 at 10:23












            • Is the expression MyClass() << 5 valid? I don't think so! gcc and clang properly reject this expression. mvc accepts it ... You cannot bind rvalue to noconst lvalue

              – Biagio Festa
              Jun 25 at 11:21







            • 2





              @BiagioFesta Yes I said that in my answer

              – Lightness Races in Orbit
              Jun 25 at 11:31

















            • then would return std::move(MyClass() << 5); work?

              – RomCoo
              Jun 25 at 10:22











            • @RomCoo I believe so. However that is very strange to look at and suggests there's a better way to accomplish this.

              – Lightness Races in Orbit
              Jun 25 at 10:23












            • Is the expression MyClass() << 5 valid? I don't think so! gcc and clang properly reject this expression. mvc accepts it ... You cannot bind rvalue to noconst lvalue

              – Biagio Festa
              Jun 25 at 11:21







            • 2





              @BiagioFesta Yes I said that in my answer

              – Lightness Races in Orbit
              Jun 25 at 11:31
















            then would return std::move(MyClass() << 5); work?

            – RomCoo
            Jun 25 at 10:22





            then would return std::move(MyClass() << 5); work?

            – RomCoo
            Jun 25 at 10:22













            @RomCoo I believe so. However that is very strange to look at and suggests there's a better way to accomplish this.

            – Lightness Races in Orbit
            Jun 25 at 10:23






            @RomCoo I believe so. However that is very strange to look at and suggests there's a better way to accomplish this.

            – Lightness Races in Orbit
            Jun 25 at 10:23














            Is the expression MyClass() << 5 valid? I don't think so! gcc and clang properly reject this expression. mvc accepts it ... You cannot bind rvalue to noconst lvalue

            – Biagio Festa
            Jun 25 at 11:21






            Is the expression MyClass() << 5 valid? I don't think so! gcc and clang properly reject this expression. mvc accepts it ... You cannot bind rvalue to noconst lvalue

            – Biagio Festa
            Jun 25 at 11:21





            2




            2





            @BiagioFesta Yes I said that in my answer

            – Lightness Races in Orbit
            Jun 25 at 11:31





            @BiagioFesta Yes I said that in my answer

            – Lightness Races in Orbit
            Jun 25 at 11:31













            7














            Your operator return by MyClass&. So you are returning an lvalue, not an rvalue that can be moved automatically.



            You can avoid the copy by relying on the standard guarantees regarding NRVO.



            MyClass func()

            MyClass m;
            m << 5;
            return m;



            This will either elide the object entirely, or move it. All on account of it being a function local object.




            Another option, seeing as you are trying to call operator<< on an rvalue, is to supply an overload dealing in rvalue references.



            MyClass&& operator<<(MyClass&& target, int i) 
            target << i; // Reuse the operator you have, here target is an lvalue
            return std::move(target);



            That will make MyClass() << 5 itself well formed (see the other answer for why it isn't), and return an xvalue from which the return object may be constructed. Though such and overload for operator<< is not commonly seen.






            share|improve this answer

























            • Presumably he wanted to return the result of operator<<, not m.

              – David Schwartz
              Jun 25 at 10:06











            • @DavidSchwartz - One can also presume operator<< is idiomatic and ends in return target; The signature would suggest so.

              – StoryTeller
              Jun 25 at 10:08











            • But the lack of a const on the first parameter, at least to me, suggests otherwise. Also, that would really not be idiomatic. It'd be odd if i<<3 returned i.

              – David Schwartz
              Jun 25 at 10:08












            • @DavidSchwartz - That's quite a leap either way. I think we both hastened to answer instead of asking the OP for a better example.

              – StoryTeller
              Jun 25 at 10:09






            • 1





              @DavidSchwartz - Not idiomatic, what? That's what stream operators do.

              – StoryTeller
              Jun 25 at 10:09















            7














            Your operator return by MyClass&. So you are returning an lvalue, not an rvalue that can be moved automatically.



            You can avoid the copy by relying on the standard guarantees regarding NRVO.



            MyClass func()

            MyClass m;
            m << 5;
            return m;



            This will either elide the object entirely, or move it. All on account of it being a function local object.




            Another option, seeing as you are trying to call operator<< on an rvalue, is to supply an overload dealing in rvalue references.



            MyClass&& operator<<(MyClass&& target, int i) 
            target << i; // Reuse the operator you have, here target is an lvalue
            return std::move(target);



            That will make MyClass() << 5 itself well formed (see the other answer for why it isn't), and return an xvalue from which the return object may be constructed. Though such and overload for operator<< is not commonly seen.






            share|improve this answer

























            • Presumably he wanted to return the result of operator<<, not m.

              – David Schwartz
              Jun 25 at 10:06











            • @DavidSchwartz - One can also presume operator<< is idiomatic and ends in return target; The signature would suggest so.

              – StoryTeller
              Jun 25 at 10:08











            • But the lack of a const on the first parameter, at least to me, suggests otherwise. Also, that would really not be idiomatic. It'd be odd if i<<3 returned i.

              – David Schwartz
              Jun 25 at 10:08












            • @DavidSchwartz - That's quite a leap either way. I think we both hastened to answer instead of asking the OP for a better example.

              – StoryTeller
              Jun 25 at 10:09






            • 1





              @DavidSchwartz - Not idiomatic, what? That's what stream operators do.

              – StoryTeller
              Jun 25 at 10:09













            7












            7








            7







            Your operator return by MyClass&. So you are returning an lvalue, not an rvalue that can be moved automatically.



            You can avoid the copy by relying on the standard guarantees regarding NRVO.



            MyClass func()

            MyClass m;
            m << 5;
            return m;



            This will either elide the object entirely, or move it. All on account of it being a function local object.




            Another option, seeing as you are trying to call operator<< on an rvalue, is to supply an overload dealing in rvalue references.



            MyClass&& operator<<(MyClass&& target, int i) 
            target << i; // Reuse the operator you have, here target is an lvalue
            return std::move(target);



            That will make MyClass() << 5 itself well formed (see the other answer for why it isn't), and return an xvalue from which the return object may be constructed. Though such and overload for operator<< is not commonly seen.






            share|improve this answer















            Your operator return by MyClass&. So you are returning an lvalue, not an rvalue that can be moved automatically.



            You can avoid the copy by relying on the standard guarantees regarding NRVO.



            MyClass func()

            MyClass m;
            m << 5;
            return m;



            This will either elide the object entirely, or move it. All on account of it being a function local object.




            Another option, seeing as you are trying to call operator<< on an rvalue, is to supply an overload dealing in rvalue references.



            MyClass&& operator<<(MyClass&& target, int i) 
            target << i; // Reuse the operator you have, here target is an lvalue
            return std::move(target);



            That will make MyClass() << 5 itself well formed (see the other answer for why it isn't), and return an xvalue from which the return object may be constructed. Though such and overload for operator<< is not commonly seen.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jun 25 at 10:17

























            answered Jun 25 at 10:04









            StoryTellerStoryTeller

            113k18 gold badges243 silver badges308 bronze badges




            113k18 gold badges243 silver badges308 bronze badges












            • Presumably he wanted to return the result of operator<<, not m.

              – David Schwartz
              Jun 25 at 10:06











            • @DavidSchwartz - One can also presume operator<< is idiomatic and ends in return target; The signature would suggest so.

              – StoryTeller
              Jun 25 at 10:08











            • But the lack of a const on the first parameter, at least to me, suggests otherwise. Also, that would really not be idiomatic. It'd be odd if i<<3 returned i.

              – David Schwartz
              Jun 25 at 10:08












            • @DavidSchwartz - That's quite a leap either way. I think we both hastened to answer instead of asking the OP for a better example.

              – StoryTeller
              Jun 25 at 10:09






            • 1





              @DavidSchwartz - Not idiomatic, what? That's what stream operators do.

              – StoryTeller
              Jun 25 at 10:09

















            • Presumably he wanted to return the result of operator<<, not m.

              – David Schwartz
              Jun 25 at 10:06











            • @DavidSchwartz - One can also presume operator<< is idiomatic and ends in return target; The signature would suggest so.

              – StoryTeller
              Jun 25 at 10:08











            • But the lack of a const on the first parameter, at least to me, suggests otherwise. Also, that would really not be idiomatic. It'd be odd if i<<3 returned i.

              – David Schwartz
              Jun 25 at 10:08












            • @DavidSchwartz - That's quite a leap either way. I think we both hastened to answer instead of asking the OP for a better example.

              – StoryTeller
              Jun 25 at 10:09






            • 1





              @DavidSchwartz - Not idiomatic, what? That's what stream operators do.

              – StoryTeller
              Jun 25 at 10:09
















            Presumably he wanted to return the result of operator<<, not m.

            – David Schwartz
            Jun 25 at 10:06





            Presumably he wanted to return the result of operator<<, not m.

            – David Schwartz
            Jun 25 at 10:06













            @DavidSchwartz - One can also presume operator<< is idiomatic and ends in return target; The signature would suggest so.

            – StoryTeller
            Jun 25 at 10:08





            @DavidSchwartz - One can also presume operator<< is idiomatic and ends in return target; The signature would suggest so.

            – StoryTeller
            Jun 25 at 10:08













            But the lack of a const on the first parameter, at least to me, suggests otherwise. Also, that would really not be idiomatic. It'd be odd if i<<3 returned i.

            – David Schwartz
            Jun 25 at 10:08






            But the lack of a const on the first parameter, at least to me, suggests otherwise. Also, that would really not be idiomatic. It'd be odd if i<<3 returned i.

            – David Schwartz
            Jun 25 at 10:08














            @DavidSchwartz - That's quite a leap either way. I think we both hastened to answer instead of asking the OP for a better example.

            – StoryTeller
            Jun 25 at 10:09





            @DavidSchwartz - That's quite a leap either way. I think we both hastened to answer instead of asking the OP for a better example.

            – StoryTeller
            Jun 25 at 10:09




            1




            1





            @DavidSchwartz - Not idiomatic, what? That's what stream operators do.

            – StoryTeller
            Jun 25 at 10:09





            @DavidSchwartz - Not idiomatic, what? That's what stream operators do.

            – StoryTeller
            Jun 25 at 10:09











            1














            Your operator<< takes its first parameter as a non-const reference. You can't bind a non-const reference to a temporary. But MyClass() returns the newly-created instance as a temporary.



            Also, while func returns a value, operator<< returns a reference. So what else can it do but make a copy to return?






            share|improve this answer



























              1














              Your operator<< takes its first parameter as a non-const reference. You can't bind a non-const reference to a temporary. But MyClass() returns the newly-created instance as a temporary.



              Also, while func returns a value, operator<< returns a reference. So what else can it do but make a copy to return?






              share|improve this answer

























                1












                1








                1







                Your operator<< takes its first parameter as a non-const reference. You can't bind a non-const reference to a temporary. But MyClass() returns the newly-created instance as a temporary.



                Also, while func returns a value, operator<< returns a reference. So what else can it do but make a copy to return?






                share|improve this answer













                Your operator<< takes its first parameter as a non-const reference. You can't bind a non-const reference to a temporary. But MyClass() returns the newly-created instance as a temporary.



                Also, while func returns a value, operator<< returns a reference. So what else can it do but make a copy to return?







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jun 25 at 10:06









                David SchwartzDavid Schwartz

                143k14 gold badges152 silver badges237 bronze badges




                143k14 gold badges152 silver badges237 bronze badges



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56751481%2fwhy-is-the-copy-constructor-called-instead-of-the-move-constructor-when-returnin%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Grendel Contents Story Scholarship Depictions Notes References Navigation menu10.1093/notesj/gjn112Berserkeree

                    Area configuration aggregation error after install Porto themeMagento 2.1 CE Installed but front/backend not loading/workingCSS not loading on page within Magento 2 pageCannot install module in Magento 2no commands defined in the “setup” namespace. in Magento2Magento 2: Static files are present but shows 404Why do i have to always run the commands to clean cache in Magento 2.1.8?Failure reason: 'Unable to unserialize value.'Error 500 after magento migrationIn production mode the site does not loadMagento 2 : Error 500 after installing

                    Middle Expansion Olielle Resaix Definition: Uttering songs of triumph shouting with joy triumphant exulting Sejunction Journal 붙다 달 고급 품목 외출 The stretch trades the screeching tin. Definition: The act of speaking with a drawl a drawl Cough Sand Definition: An uproar a quarrel a noisy outbreak Shake Iron Publicize Horse House Baby 사과 Resaix Flaggy Jelly Temporary Unequaled Puppet A drop in the bucket Shrew 성격 회원 성질 미팅 The burn frames the tacky quality. Materialistic The smoke reduces the way. Yammoe Nondescript Cheek 얼굴 배 약하다 날리다 타다 The illegal country shows the iron. Help Rule Drearien Smoke Teaching Meaty Wasp Abraham Lincoln Jaws 진심 수리하다 Size Cork Idea Convert Think Lark John Lennon 거울 청소 군 추천하다 아이스크림