Tuesday, October 10, 2017

C++17: [[nodiscard]] Attribute

C++17 added the [[nodiscard]] attribute to specify that any function call that returns a value, has that value stored in the calling code, or else a warning is given: Here is an example:

[[nodiscard]]
int func()
{
  return 0;
}

int main()
{
  func();
  return 0;
}
// Compiler Warning: warning C4834: discarding return value of function
                                                with 'nodiscard' attribute
References:
https://en.wikipedia.org/wiki/C%2B%2B17
https://infektor.net/posts/2017-01-19-using-cpp17-attributes-today.html

No comments:

Post a Comment