Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the statement “(void)startGuardBegin;” do? [duplicate]

Tags:

c++

Possible Duplicate:
What does “(void) new” mean in C++?

I'm not familiar with C++ and I don't understand the line right after the method signature:

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

What's (void)startGuardBegin;? A method invokation?

like image 959
aneuryzm Avatar asked Jun 18 '12 08:06

aneuryzm


3 Answers

It tells the compiler that the argument is unused and thus it shouldn't show an "unused argument" warning.

While compilers such as GCC usually have other ways (int startGuardBegin __attribute__ ((unused))) to indicated this, usually somehow in the function header, casting it to (void) does not rely on any compiler-specific features.

like image 195
ThiefMaster Avatar answered Dec 16 '22 13:12

ThiefMaster


It doesn't do anything.

Instead, it specifies to the reader and to any static analysis tool that startGuardBegin is unused in the function and that this is OK and expected.

Static analysis tools will warn if a parameter is unused in a function as this indicates a possible bug. If the parameter can't be removed from the signature (if it is used in debug code, or is required for compatibility or future behaviour) then using the parameter in a no-effect statement will prevent the warning. However, just using it in a statement startGuardBegin; will trigger another warning (as the value is discarded), so casting it to void prevents that.

like image 30
ecatmur Avatar answered Dec 16 '22 11:12

ecatmur


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

The Standard(2003) says in §5.2.9/4 says,

Any expression can be explicitly converted to type “cv void.” The expression value is discarded.

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

//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 make it shorter as:

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

Its also okay.

like image 36
Nawaz Avatar answered Dec 16 '22 11:12

Nawaz