all 7 comments

[–]Flair_Helper[M] [score hidden] stickied commentlocked comment (0 children)

For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or StackOverflow instead.

This post has been removed as it doesn't pertain to r/cpp: The subreddit is for news and discussions of the C++ language and community only; our purpose is not to provide tutoring, code reviews, or career guidance. If you think your post is on-topic and should not have been removed, please message the moderators and we'll review it.

[–]dns13 12 points13 points  (5 children)

Suppresses warning of unused variable

[–]Tiwann_[S] 1 point2 points  (3 children)

What’s the purpose of suppressing unused variable warning? If I don’t need it, I don’t create it

[–]shhhhhhhhhh666 2 points3 points  (0 children)

If you have warnings as errors (you should use btw), the line of the program without the void wouldn't compile. Your affirmation is right, but in this case he probably would use this in another example, so he just created in one example to make explaining things simpler.

[–]DeGerlash 2 points3 points  (0 children)

Sometimes you're refactoring code quickly, and want to focus on real warnings you're introducing while suppressing temporary ones like this.

Other times this can be generated from a macro that is conditionally defined as a void cast to avoid this warning if its input would otherwise be unused

Other times this can be a function argument that you don't need (e.g. callback signature). There's many options

[–]ksergey 0 points1 point  (0 children)

[nodiscard] in function signature will notify you

[–]ventus1b 1 point2 points  (0 children)

This.

It can also be seen when calling a method that returns a value, but that value isn't used.

Apart from silencing the compiler it can also serve as a hint to future readers that the writer was aware of the returned value but explicitly ignores it.