c++c++17language-lawyerc++20nodiscard

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


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?


Solution

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