Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why c local variable need translated to void?

Tags:

c++

recently I'm studying a project in github,and I found this.why there is a "(void)n" in the end?

void CurrentThread::cacheTid()
{
  if (t_cachedTid == 0)
  {
    t_cachedTid = detail::gettid();
    int n = snprintf(t_tidString, sizeof t_tidString, "%5d ", t_cachedTid);
    assert(n == 6); (void) n;
  }
}
like image 896
JoeyMiu Avatar asked Jun 06 '26 21:06

JoeyMiu


2 Answers

(void)n is effectively a no-op. However, it will prevent the compiler from issuing an "unused variable" warning when assertions are turned off and the preceding assert() is compiled out.

like image 138
NPE Avatar answered Jun 09 '26 11:06

NPE


This construction suppresses warning about declared, but never used variable

like image 35
borisbn Avatar answered Jun 09 '26 11:06

borisbn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!