Why is std::move not [[nodiscard]] in C++20?Why can templates only be implemented in the header file?Why is “using namespace std” considered bad practice?What are move semantics?Why are elementwise additions much faster in separate loops than in a combined loop?Why is reading lines from stdin much slower in C++ than Python?Why is it faster to process a sorted array than an unsorted array?Why should I use a pointer rather than the object itself?enum to string in modern C++11 / C++14 / C++17 and future C++20Why is `std::reference_wrapper` deprecated in c++17 and removed in c++20?Why use std::make_unique in C++17?

What makes accurate emulation of old systems a difficult task?

Critique of timeline aesthetic

What language was spoken in East Asia before Proto-Turkic?

simple conditions equation

Why was the Spitfire's elliptical wing almost uncopied by other aircraft of World War 2?

Exchange,swap or switch

Error message with tabularx

Why does nature favour the Laplacian?

Who is the Umpire in this picture?

French for 'It must be my imagination'?

Sci fi novel series with instant travel between planets through gates. A river runs through the gates

Meaning of Bloch representation

How much cash can I safely carry into the USA and avoid civil forfeiture?

How to get a plain text file version of a CP/M .BAS (M-BASIC) program?

Does a semiconductor follow Ohm's law?

Examples of subgroups where it's nontrivial to show closure under multiplication?

Can someone publish a story that happened to you?

Do I have an "anti-research" personality?

how to sum variables from file in bash

how to find the equation of a circle given points of the circle

How to verbalise code in Mathematica?

Unexpected email from Yorkshire Bank

What was the first Intel x86 processor with "Base + Index * Scale + Displacement" addressing mode?

Was there a shared-world project before "Thieves World"?



Why is std::move not [[nodiscard]] in C++20?


Why can templates only be implemented in the header file?Why is “using namespace std” considered bad practice?What are move semantics?Why are elementwise additions much faster in separate loops than in a combined loop?Why is reading lines from stdin much slower in C++ than Python?Why is it faster to process a sorted array than an unsorted array?Why should I use a pointer rather than the object itself?enum to string in modern C++11 / C++14 / C++17 and future C++20Why is `std::reference_wrapper` deprecated in c++17 and removed in c++20?Why use std::make_unique in C++17?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








36















I've recently read about [[nodiscard]] in C++17, and as far as I understand it's a new feature (design by contract?) which forces you to use the return value. This makes sense for controversial functions like std::launder (nodiscard since C++20), but I wonder why std::move isn't defined like so in C++17/20. Do you know a good reason or is it because C++20 isn't finalised yet?










share|improve this question



















  • 1





    I know a reason that makes sense to me, but unless there's an official document explicitly saying why it isn't, that just turns the question to be opinion-based.

    – Sebastian Redl
    Apr 20 at 10:24






  • 8





    Because absolutely nothing bad (or at all) happens when you don't use it.

    – Sebastian Redl
    Apr 20 at 10:33






  • 6





    @SebastianRedl: similarly, nothing useful happens. It's the same as writing an empty statement, so [[nodiscard]] would help diagnose bugs. Also, nothing bad happens when vector::empty() is ignored, but that is marked [[nodiscard]] for obvious reasons.

    – Vittorio Romeo
    Apr 20 at 10:39






  • 5





    @SebastianRedl That sounds like a great reason to mark it [[nodiscard]]: "Hey, you did something completely pointless. Did you mean to do something else?"

    – Barry
    Apr 20 at 18:53






  • 4





    @bbalchev std::move doesn't move. Passing an object through std::move and ignoring the result does absolutely nothing.

    – tkausl
    Apr 20 at 23:04

















36















I've recently read about [[nodiscard]] in C++17, and as far as I understand it's a new feature (design by contract?) which forces you to use the return value. This makes sense for controversial functions like std::launder (nodiscard since C++20), but I wonder why std::move isn't defined like so in C++17/20. Do you know a good reason or is it because C++20 isn't finalised yet?










share|improve this question



















  • 1





    I know a reason that makes sense to me, but unless there's an official document explicitly saying why it isn't, that just turns the question to be opinion-based.

    – Sebastian Redl
    Apr 20 at 10:24






  • 8





    Because absolutely nothing bad (or at all) happens when you don't use it.

    – Sebastian Redl
    Apr 20 at 10:33






  • 6





    @SebastianRedl: similarly, nothing useful happens. It's the same as writing an empty statement, so [[nodiscard]] would help diagnose bugs. Also, nothing bad happens when vector::empty() is ignored, but that is marked [[nodiscard]] for obvious reasons.

    – Vittorio Romeo
    Apr 20 at 10:39






  • 5





    @SebastianRedl That sounds like a great reason to mark it [[nodiscard]]: "Hey, you did something completely pointless. Did you mean to do something else?"

    – Barry
    Apr 20 at 18:53






  • 4





    @bbalchev std::move doesn't move. Passing an object through std::move and ignoring the result does absolutely nothing.

    – tkausl
    Apr 20 at 23:04













36












36








36


3






I've recently read about [[nodiscard]] in C++17, and as far as I understand it's a new feature (design by contract?) which forces you to use the return value. This makes sense for controversial functions like std::launder (nodiscard since C++20), but I wonder why std::move isn't defined like so in C++17/20. Do you know a good reason or is it because C++20 isn't finalised yet?










share|improve this question
















I've recently read about [[nodiscard]] in C++17, and as far as I understand it's a new feature (design by contract?) which forces you to use the return value. This makes sense for controversial functions like std::launder (nodiscard since C++20), but I wonder why std::move isn't defined like so in C++17/20. Do you know a good reason or is it because C++20 isn't finalised yet?







c++ language-lawyer c++17 c++20






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 20 at 19:21







bbalchev

















asked Apr 20 at 10:07









bbalchevbbalchev

3971416




3971416







  • 1





    I know a reason that makes sense to me, but unless there's an official document explicitly saying why it isn't, that just turns the question to be opinion-based.

    – Sebastian Redl
    Apr 20 at 10:24






  • 8





    Because absolutely nothing bad (or at all) happens when you don't use it.

    – Sebastian Redl
    Apr 20 at 10:33






  • 6





    @SebastianRedl: similarly, nothing useful happens. It's the same as writing an empty statement, so [[nodiscard]] would help diagnose bugs. Also, nothing bad happens when vector::empty() is ignored, but that is marked [[nodiscard]] for obvious reasons.

    – Vittorio Romeo
    Apr 20 at 10:39






  • 5





    @SebastianRedl That sounds like a great reason to mark it [[nodiscard]]: "Hey, you did something completely pointless. Did you mean to do something else?"

    – Barry
    Apr 20 at 18:53






  • 4





    @bbalchev std::move doesn't move. Passing an object through std::move and ignoring the result does absolutely nothing.

    – tkausl
    Apr 20 at 23:04












  • 1





    I know a reason that makes sense to me, but unless there's an official document explicitly saying why it isn't, that just turns the question to be opinion-based.

    – Sebastian Redl
    Apr 20 at 10:24






  • 8





    Because absolutely nothing bad (or at all) happens when you don't use it.

    – Sebastian Redl
    Apr 20 at 10:33






  • 6





    @SebastianRedl: similarly, nothing useful happens. It's the same as writing an empty statement, so [[nodiscard]] would help diagnose bugs. Also, nothing bad happens when vector::empty() is ignored, but that is marked [[nodiscard]] for obvious reasons.

    – Vittorio Romeo
    Apr 20 at 10:39






  • 5





    @SebastianRedl That sounds like a great reason to mark it [[nodiscard]]: "Hey, you did something completely pointless. Did you mean to do something else?"

    – Barry
    Apr 20 at 18:53






  • 4





    @bbalchev std::move doesn't move. Passing an object through std::move and ignoring the result does absolutely nothing.

    – tkausl
    Apr 20 at 23:04







1




1





I know a reason that makes sense to me, but unless there's an official document explicitly saying why it isn't, that just turns the question to be opinion-based.

– Sebastian Redl
Apr 20 at 10:24





I know a reason that makes sense to me, but unless there's an official document explicitly saying why it isn't, that just turns the question to be opinion-based.

– Sebastian Redl
Apr 20 at 10:24




8




8





Because absolutely nothing bad (or at all) happens when you don't use it.

– Sebastian Redl
Apr 20 at 10:33





Because absolutely nothing bad (or at all) happens when you don't use it.

– Sebastian Redl
Apr 20 at 10:33




6




6





@SebastianRedl: similarly, nothing useful happens. It's the same as writing an empty statement, so [[nodiscard]] would help diagnose bugs. Also, nothing bad happens when vector::empty() is ignored, but that is marked [[nodiscard]] for obvious reasons.

– Vittorio Romeo
Apr 20 at 10:39





@SebastianRedl: similarly, nothing useful happens. It's the same as writing an empty statement, so [[nodiscard]] would help diagnose bugs. Also, nothing bad happens when vector::empty() is ignored, but that is marked [[nodiscard]] for obvious reasons.

– Vittorio Romeo
Apr 20 at 10:39




5




5





@SebastianRedl That sounds like a great reason to mark it [[nodiscard]]: "Hey, you did something completely pointless. Did you mean to do something else?"

– Barry
Apr 20 at 18:53





@SebastianRedl That sounds like a great reason to mark it [[nodiscard]]: "Hey, you did something completely pointless. Did you mean to do something else?"

– Barry
Apr 20 at 18:53




4




4





@bbalchev std::move doesn't move. Passing an object through std::move and ignoring the result does absolutely nothing.

– tkausl
Apr 20 at 23:04





@bbalchev std::move doesn't move. Passing an object through std::move and ignoring the result does absolutely nothing.

– tkausl
Apr 20 at 23:04












2 Answers
2






active

oldest

votes


















26














AFAIK P0600R1 is the only proposal for adding [[nodiscard]] to the standard library that was applied to C++20. From that paper:




We suggest a conservative approach:



[...]



It should not be added when:



  • [...]

  • not using the return value makes no sense but doesn’t hurt and is usually not an error

  • [...]

So, [[nodiscard]] should not signal bad code if this



  • [...]

  • doesn’t hurt and probably no state change was meant that doesn’t happen



So the reason is that the standard library uses a conservative approach and a more aggresive one is not yet proposed.






share|improve this answer


















  • 3





    I dunno. Not using the return value of std::move is always an error in my book, and should thus be nodiscard according to your citation: it either implies that the user forgot to use the return value, or that the call is unnecessary, since it has no effect.

    – Konrad Rudolph
    Apr 20 at 23:21



















31














The MSVC standard library team went ahead and added several thousand instances of [[nodiscard]] since VS 2017 15.6, and have reported wild success with it (both in terms of finding lots of bugs and generating no user complaints). The criteria they described were approximately:



  1. Pure observers, e.g. vector::size(), vector::empty, and even std::count_if()

  2. Things that acquire raw resources, e.g. allocate()

  3. Functions where discarding the return value is extremely likely to lead to incorrect code, e.g. std::remove()

MSVC does mark both std::move() and std::forward() as [[nodiscard]] following these criteria.



While it's not officially annotated as such in the standard, it seems to provide clear user benefit and it's more a question of crafting such a paper to mark all the right things [[nodiscard]] (again, several thousand instances from MSVC) and apply them -- it's not complex work per se, but the volume is large. In the meantime, maybe prod your favorite standard library vendor and ask them to [[nodiscard]] lots of stuff?






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%2f55772424%2fwhy-is-stdmove-not-nodiscard-in-c20%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    26














    AFAIK P0600R1 is the only proposal for adding [[nodiscard]] to the standard library that was applied to C++20. From that paper:




    We suggest a conservative approach:



    [...]



    It should not be added when:



    • [...]

    • not using the return value makes no sense but doesn’t hurt and is usually not an error

    • [...]

    So, [[nodiscard]] should not signal bad code if this



    • [...]

    • doesn’t hurt and probably no state change was meant that doesn’t happen



    So the reason is that the standard library uses a conservative approach and a more aggresive one is not yet proposed.






    share|improve this answer


















    • 3





      I dunno. Not using the return value of std::move is always an error in my book, and should thus be nodiscard according to your citation: it either implies that the user forgot to use the return value, or that the call is unnecessary, since it has no effect.

      – Konrad Rudolph
      Apr 20 at 23:21
















    26














    AFAIK P0600R1 is the only proposal for adding [[nodiscard]] to the standard library that was applied to C++20. From that paper:




    We suggest a conservative approach:



    [...]



    It should not be added when:



    • [...]

    • not using the return value makes no sense but doesn’t hurt and is usually not an error

    • [...]

    So, [[nodiscard]] should not signal bad code if this



    • [...]

    • doesn’t hurt and probably no state change was meant that doesn’t happen



    So the reason is that the standard library uses a conservative approach and a more aggresive one is not yet proposed.






    share|improve this answer


















    • 3





      I dunno. Not using the return value of std::move is always an error in my book, and should thus be nodiscard according to your citation: it either implies that the user forgot to use the return value, or that the call is unnecessary, since it has no effect.

      – Konrad Rudolph
      Apr 20 at 23:21














    26












    26








    26







    AFAIK P0600R1 is the only proposal for adding [[nodiscard]] to the standard library that was applied to C++20. From that paper:




    We suggest a conservative approach:



    [...]



    It should not be added when:



    • [...]

    • not using the return value makes no sense but doesn’t hurt and is usually not an error

    • [...]

    So, [[nodiscard]] should not signal bad code if this



    • [...]

    • doesn’t hurt and probably no state change was meant that doesn’t happen



    So the reason is that the standard library uses a conservative approach and a more aggresive one is not yet proposed.






    share|improve this answer













    AFAIK P0600R1 is the only proposal for adding [[nodiscard]] to the standard library that was applied to C++20. From that paper:




    We suggest a conservative approach:



    [...]



    It should not be added when:



    • [...]

    • not using the return value makes no sense but doesn’t hurt and is usually not an error

    • [...]

    So, [[nodiscard]] should not signal bad code if this



    • [...]

    • doesn’t hurt and probably no state change was meant that doesn’t happen



    So the reason is that the standard library uses a conservative approach and a more aggresive one is not yet proposed.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Apr 20 at 13:05









    cpplearnercpplearner

    6,10622543




    6,10622543







    • 3





      I dunno. Not using the return value of std::move is always an error in my book, and should thus be nodiscard according to your citation: it either implies that the user forgot to use the return value, or that the call is unnecessary, since it has no effect.

      – Konrad Rudolph
      Apr 20 at 23:21













    • 3





      I dunno. Not using the return value of std::move is always an error in my book, and should thus be nodiscard according to your citation: it either implies that the user forgot to use the return value, or that the call is unnecessary, since it has no effect.

      – Konrad Rudolph
      Apr 20 at 23:21








    3




    3





    I dunno. Not using the return value of std::move is always an error in my book, and should thus be nodiscard according to your citation: it either implies that the user forgot to use the return value, or that the call is unnecessary, since it has no effect.

    – Konrad Rudolph
    Apr 20 at 23:21






    I dunno. Not using the return value of std::move is always an error in my book, and should thus be nodiscard according to your citation: it either implies that the user forgot to use the return value, or that the call is unnecessary, since it has no effect.

    – Konrad Rudolph
    Apr 20 at 23:21














    31














    The MSVC standard library team went ahead and added several thousand instances of [[nodiscard]] since VS 2017 15.6, and have reported wild success with it (both in terms of finding lots of bugs and generating no user complaints). The criteria they described were approximately:



    1. Pure observers, e.g. vector::size(), vector::empty, and even std::count_if()

    2. Things that acquire raw resources, e.g. allocate()

    3. Functions where discarding the return value is extremely likely to lead to incorrect code, e.g. std::remove()

    MSVC does mark both std::move() and std::forward() as [[nodiscard]] following these criteria.



    While it's not officially annotated as such in the standard, it seems to provide clear user benefit and it's more a question of crafting such a paper to mark all the right things [[nodiscard]] (again, several thousand instances from MSVC) and apply them -- it's not complex work per se, but the volume is large. In the meantime, maybe prod your favorite standard library vendor and ask them to [[nodiscard]] lots of stuff?






    share|improve this answer





























      31














      The MSVC standard library team went ahead and added several thousand instances of [[nodiscard]] since VS 2017 15.6, and have reported wild success with it (both in terms of finding lots of bugs and generating no user complaints). The criteria they described were approximately:



      1. Pure observers, e.g. vector::size(), vector::empty, and even std::count_if()

      2. Things that acquire raw resources, e.g. allocate()

      3. Functions where discarding the return value is extremely likely to lead to incorrect code, e.g. std::remove()

      MSVC does mark both std::move() and std::forward() as [[nodiscard]] following these criteria.



      While it's not officially annotated as such in the standard, it seems to provide clear user benefit and it's more a question of crafting such a paper to mark all the right things [[nodiscard]] (again, several thousand instances from MSVC) and apply them -- it's not complex work per se, but the volume is large. In the meantime, maybe prod your favorite standard library vendor and ask them to [[nodiscard]] lots of stuff?






      share|improve this answer



























        31












        31








        31







        The MSVC standard library team went ahead and added several thousand instances of [[nodiscard]] since VS 2017 15.6, and have reported wild success with it (both in terms of finding lots of bugs and generating no user complaints). The criteria they described were approximately:



        1. Pure observers, e.g. vector::size(), vector::empty, and even std::count_if()

        2. Things that acquire raw resources, e.g. allocate()

        3. Functions where discarding the return value is extremely likely to lead to incorrect code, e.g. std::remove()

        MSVC does mark both std::move() and std::forward() as [[nodiscard]] following these criteria.



        While it's not officially annotated as such in the standard, it seems to provide clear user benefit and it's more a question of crafting such a paper to mark all the right things [[nodiscard]] (again, several thousand instances from MSVC) and apply them -- it's not complex work per se, but the volume is large. In the meantime, maybe prod your favorite standard library vendor and ask them to [[nodiscard]] lots of stuff?






        share|improve this answer















        The MSVC standard library team went ahead and added several thousand instances of [[nodiscard]] since VS 2017 15.6, and have reported wild success with it (both in terms of finding lots of bugs and generating no user complaints). The criteria they described were approximately:



        1. Pure observers, e.g. vector::size(), vector::empty, and even std::count_if()

        2. Things that acquire raw resources, e.g. allocate()

        3. Functions where discarding the return value is extremely likely to lead to incorrect code, e.g. std::remove()

        MSVC does mark both std::move() and std::forward() as [[nodiscard]] following these criteria.



        While it's not officially annotated as such in the standard, it seems to provide clear user benefit and it's more a question of crafting such a paper to mark all the right things [[nodiscard]] (again, several thousand instances from MSVC) and apply them -- it's not complex work per se, but the volume is large. In the meantime, maybe prod your favorite standard library vendor and ask them to [[nodiscard]] lots of stuff?







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 20 at 21:44

























        answered Apr 20 at 18:52









        BarryBarry

        188k21337618




        188k21337618



























            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%2f55772424%2fwhy-is-stdmove-not-nodiscard-in-c20%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

            Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

            Bruxelas-Capital Índice Historia | Composición | Situación lingüística | Clima | Cidades irmandadas | Notas | Véxase tamén | Menú de navegacióneO uso das linguas en Bruxelas e a situación do neerlandés"Rexión de Bruxelas Capital"o orixinalSitio da rexiónPáxina de Bruselas no sitio da Oficina de Promoción Turística de Valonia e BruxelasMapa Interactivo da Rexión de Bruxelas-CapitaleeWorldCat332144929079854441105155190212ID28008674080552-90000 0001 0666 3698n94104302ID540940339365017018237

            What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company