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;
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
|
show 6 more comments
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
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 whenvector::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
@bbalchevstd::move
doesn't move. Passing an object throughstd::move
and ignoring the result does absolutely nothing.
– tkausl
Apr 20 at 23:04
|
show 6 more comments
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
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
c++ language-lawyer c++17 c++20
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 whenvector::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
@bbalchevstd::move
doesn't move. Passing an object throughstd::move
and ignoring the result does absolutely nothing.
– tkausl
Apr 20 at 23:04
|
show 6 more comments
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 whenvector::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
@bbalchevstd::move
doesn't move. Passing an object throughstd::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
|
show 6 more comments
2 Answers
2
active
oldest
votes
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.
3
I dunno. Not using the return value ofstd::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
add a comment |
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:
- Pure observers, e.g.
vector::size()
,vector::empty
, and evenstd::count_if()
- Things that acquire raw resources, e.g.
allocate()
- 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?
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
3
I dunno. Not using the return value ofstd::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
add a comment |
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.
3
I dunno. Not using the return value ofstd::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
add a comment |
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.
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.
answered Apr 20 at 13:05
cpplearnercpplearner
6,10622543
6,10622543
3
I dunno. Not using the return value ofstd::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
add a comment |
3
I dunno. Not using the return value ofstd::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
add a comment |
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:
- Pure observers, e.g.
vector::size()
,vector::empty
, and evenstd::count_if()
- Things that acquire raw resources, e.g.
allocate()
- 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?
add a comment |
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:
- Pure observers, e.g.
vector::size()
,vector::empty
, and evenstd::count_if()
- Things that acquire raw resources, e.g.
allocate()
- 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?
add a comment |
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:
- Pure observers, e.g.
vector::size()
,vector::empty
, and evenstd::count_if()
- Things that acquire raw resources, e.g.
allocate()
- 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?
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:
- Pure observers, e.g.
vector::size()
,vector::empty
, and evenstd::count_if()
- Things that acquire raw resources, e.g.
allocate()
- 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?
edited Apr 20 at 21:44
answered Apr 20 at 18:52
BarryBarry
188k21337618
188k21337618
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 whenvector::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 throughstd::move
and ignoring the result does absolutely nothing.– tkausl
Apr 20 at 23:04