What does the statement ("void) startGuardBegin" do; do?

Possible duplicate:
What does "(void) new" mean in C ++ mean?

I am not familiar with C ++, and I do not understand the line immediately after signing the method:

int EAN13Reader::decodeMiddle(Ref<BitArray> row, int startGuardBegin, int startGuardEnd, std::string& resultString) { (void)startGuardBegin; ... } 

What (void)startGuardBegin; ? Method call?

+6
source share
3 answers

It tells the compiler that the argument is not used, and therefore it should not display the warning “unused argument”.

While compilers such as GCC usually have other ways ( int startGuardBegin __attribute__ ((unused)) ) to indicate this, as a rule, somehow in the function header, dropping it to (void) , does not rely on which any compiler-specific functions.

+4
source

He does not do anything.

Instead, it tells the reader and any static analysis tool that startGuardBegin not used in the function and that this is normal and expected.

Static analysis tools will warn you if a parameter is not used in a function, as this indicates a possible error. If the parameter cannot be removed from the signature (if it is used in debugging code or is necessary for compatibility or future behavior), then using the parameter in an instruction without effect will prevent this warning. However, just using it in the startGuardBegin; expression startGuardBegin; , another warning is issued (when discarding the value), so casting it to void prevents this.

+3
source

void used to suppress compiler warnings for unused variables and unsaved return values ​​or expressions.

The standard (2003) states that paragraph 5.2.9 / 4 states:

Any expression can be explicitly converted to type "cv void". The value of the expression is selected .

So you can write (in C ++ style):

 //suppressing unused variable warnings static_cast<void>(unusedVar); //suppressing return value warnings static_cast<void>(fn()); //suppressing unsaved expressions static_cast<void>(a + b * 10); static_cast<void>( x &&y || z); static_cast<void>( m | n + fn()); 

All forms are valid. I usually do this shorter:

 //suppressing expressions (void)(unusedVar); (void)(fn()); (void)(x &&y || z); 

Its good too.

+1
source

Source: https://habr.com/ru/post/918337/


All Articles