Giriş
C++17 ile geliyor. Açıklaması şöyle.
Açıklaması şöyle.
Attribute kalıtım ile geçmez. Şöyle yapamayız.
C++17 ile geliyor. Açıklaması şöyle.
C++17 introduces the [[nodiscard]] attribute, which allows programmers to mark functionsnodiscard Sadece Uyarı Amaçlıdır
in a way that the compiler produces a warning if the returned object is discarded by a
a caller; the same attribute can be added to an entire class type.
Açıklaması şöyle.
The existing practice demonstrates there are cases when the programmer intentionally wants to discard the result of a nodiscard function, even though in most cases they do not. The existing nodiscard is a hint from the function designer to the function user, that immediately destroying the result is most likely not what you want, but it isn’t a straightjacket and isn’t used as such.Bazı yerlerde kullanıcıyı uyarmak gereklidir. Açıklaması şöyle.
Canonical examples include the return value of std::async where ignoring the return value would mean being not async at all, and the return value of a container’s empty() function where people sometimes confuse empty() and clear() and we don’t want them to think calling empty() has side effects if they call it by mistake.Bazı yerlerde ise gereksizdir.Açıklaması şöyle.
1.Consider a queue data type with a .pop() method that removes an element and returns a copy of the removed element. Such a method is often convenient. However, there are some cases where we only want to remove the element, without getting a copy. That is perfectly legitimate, and a warning would not be helpful. A different design (such as std::vector) splits these responsibilities, but that has other tradeoffs.Örnek
2.Consider fluent interfaces, where each operation returns the object so that further operations can be performed. In C++, the most common example is the stream insertion operator <<. It would be extremely cumbersome to add a [[nodiscard]] attribute to each and every << overload.
Attribute kalıtım ile geçmez. Şöyle yapamayız.
struct [[nodiscard]] Result {
};
struct DiscardableResult: Result {
};
Hiç yorum yok:
Yorum Gönder