logoalt Hacker News

amlutolast Sunday at 10:12 PM4 repliesview on HN

Not if you use GCC.

https://godbolt.org/z/zYdc9ej88

clang gets this right.


Replies

comexlast Sunday at 10:16 PM

It does work in GCC to suppress unused variable warnings. Just not for function calls I guess.

show 1 reply
account42last Tuesday at 10:25 AM

a) __attribute__((warn_unused_result)) is non-standard in the first place, are you looking for [[nodiscard]] - GCC does not warn on cast to void with that?

b) A return value that is explicitly marked like this is very different from an unused variable that gp suggested the cast to void idiom for. GCC does not warn on variables that are unused except for a cast to void.

Am4TIfIsER0pposlast Sunday at 10:26 PM

You've defined that function with an attribute saying not to ignore the returned value. Is it right to explicitly silence an explicit warning?

show 2 replies
gpderettalast Monday at 1:23 PM

interestingly it works for [[nodiscard]]!

and assigning to std::ignore works for both.