New [[nodiscard]] attribute usage.
-
The
[[nodiscard]]
attribute can be use either for function return and for struct. Both will return the same warning; Is there a better use case for the attribute ? Either tag the struct:struct [[nodiscard]] Patate
{
int patate;
};Patate f1()
{
return Patate();
};void main()
{
f1(); // warning C4834: discarding return value of function with 'nodiscard' attribute.
}or tag the function return.
struct Patate
{
int patate;
};[[nodiscard]] Patate f1()
{
return Patate();
};void main()
{
f1(); // warning C4834: discarding return value of function with 'nodiscard' attribute.
}I'd rather be phishing!
-
The
[[nodiscard]]
attribute can be use either for function return and for struct. Both will return the same warning; Is there a better use case for the attribute ? Either tag the struct:struct [[nodiscard]] Patate
{
int patate;
};Patate f1()
{
return Patate();
};void main()
{
f1(); // warning C4834: discarding return value of function with 'nodiscard' attribute.
}or tag the function return.
struct Patate
{
int patate;
};[[nodiscard]] Patate f1()
{
return Patate();
};void main()
{
f1(); // warning C4834: discarding return value of function with 'nodiscard' attribute.
}I'd rather be phishing!
Your function is returning by value, which your code is ignoring (discarding). A Google search for "attribute nodiscard" provides the answer. C++ attribute: nodiscard (since C++17) - cppreference.com[^]
INTP "Program testing can be used to show the presence of bugs, but never to show their absence." - Edsger Dijkstra "I have never been lost, but I will admit to being confused for several weeks. " - Daniel Boone
-
Your function is returning by value, which your code is ignoring (discarding). A Google search for "attribute nodiscard" provides the answer. C++ attribute: nodiscard (since C++17) - cppreference.com[^]
INTP "Program testing can be used to show the presence of bugs, but never to show their absence." - Edsger Dijkstra "I have never been lost, but I will admit to being confused for several weeks. " - Daniel Boone
I know that, but the description is not clear about the differences if any between the nodiscard for the struct vs for the function.
I'd rather be phishing!
-
I know that, but the description is not clear about the differences if any between the nodiscard for the struct vs for the function.
I'd rather be phishing!
Well, That attribute is weakly defined in section 10.6.7 of the standard. You can go read it yourself. The behavior is intentionally vague using words like 'encouraged' and 'discouraged'. Regardless of whether it's applied to a function or class there is no difference defined. In fact if you carefully read the document no behavior has been defined. The only section without 'encouraged' and 'discouraged' is section 1 where the attribute token "[[nodiscard]]" itself is defined. In other words in C++17 the only guarantee is that your compiler understand the [[nodiscard]] attribute. No behavior is defined. Best Wishes, -David Delaune
-
I know that, but the description is not clear about the differences if any between the nodiscard for the struct vs for the function.
I'd rather be phishing!
There is no difference you are discarding something (you didn't save it) that was marked as non discard. The whole idea should have been euthanized, it is what you get when you get a committee to talk about non existent problems. There are half a dozen real problems but they waste time on that junk. Here is the best template to fix [nodiscard]
template < typename T >
void ignore(T&&) {}Try it on your code
ignore(f1());
Now don't make me do it .... if I want to discard something I will :-)
In vino veritas
-
There is no difference you are discarding something (you didn't save it) that was marked as non discard. The whole idea should have been euthanized, it is what you get when you get a committee to talk about non existent problems. There are half a dozen real problems but they waste time on that junk. Here is the best template to fix [nodiscard]
template < typename T >
void ignore(T&&) {}Try it on your code
ignore(f1());
Now don't make me do it .... if I want to discard something I will :-)
In vino veritas
LOL :laugh: I think someone got tired of programmers who do not follow the good practice of checking returned values and decided to provide a standard way to warn them. The problem with that is those same programmers also tend to ignore warnings. ;)
INTP "Program testing can be used to show the presence of bugs, but never to show their absence." - Edsger Dijkstra "I have never been lost, but I will admit to being confused for several weeks. " - Daniel Boone
-
LOL :laugh: I think someone got tired of programmers who do not follow the good practice of checking returned values and decided to provide a standard way to warn them. The problem with that is those same programmers also tend to ignore warnings. ;)
INTP "Program testing can be used to show the presence of bugs, but never to show their absence." - Edsger Dijkstra "I have never been lost, but I will admit to being confused for several weeks. " - Daniel Boone
John R. Shaw wrote:
... same programmers also tend to ignore warnings.
But, but, if it compiles, I'd then not need to worry about the warnings, right?!