What does "(void)!ptr" do? [duplicate] - c++

This question already has answers here:
Why cast unused return values to void?
(10 answers)
Use of (apparently) empty C function
(5 answers)
Closed 9 years ago.
I see it when I read pugixml source code and I really don't know why it's there.
void foo(void* ptr) {
(void)!ptr; // What does this line do?
}

(void)ptr; is a common way to suppress "unused parameter" warnings, which may be necessary when the function signature is required to contain more parameters than the function uses (e.g. in a callback, if the 'user data' parameter is not used).
The ! is new to me, though it is superfluous in this context because the return value is just thrown away.

Related

What happens if I intentionally do not return from a non-void function? [duplicate]

This question already has answers here:
What happens when a function that returns an object ends without a return statement
(3 answers)
Omitting return statement in C++
(3 answers)
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Closed 1 year ago.
I am trying to write code with expression templates that will permit me to perform operations on objects of different types, and I am trying to customize that behavior. For this purpose, it would be convenient for me if I could write a function such as:
double ReturnVal() {};
This code produces a compiler warning "no return statement in function returning non-void". But - the function appears to do what I want. Specifically, if I run:
double x = 5;
x = ReturnVal();
I find that x remains 5. It is unchanged by the function ReturnVal(). This is what I want the code to do.
I know this seems odd, but again, this would be useful because I am trying to write code with templates, and customizing the behavior of operators when they operate on similar (but different) types objects.
So, the question is - can I rely on this behavior that "x = ReturnVal()" doesn't change x? That's what I want, and it's what I am seeing gcc do in my testing. But I am nervous that this may be undefined behavior, and I can't rely on it.

Why is void used in this manner with an inline function? [duplicate]

This question already has answers here:
Why cast unused return values to void?
(10 answers)
Closed 6 years ago.
I just ran across this in some sample code and I've never seen it used before. For an inline function which returns a type but the return value is not used, the author preceded the call with a (void). Does this actually do anything?
Example:
inline some_object& SomeClass::doSomething();
SomeClass o;
(void)o.doSomething();
This is typically done when using a tool like Lint which has been configured to issue a warning if you call a function and ignore its return value.
This is (IMO) a horrible practice that's fostered by some tools1 that give warnings about calling a function and ignoring what it returns.
The right way to deal with the problem is to give the tool a list of functions whose return values can reasonably be ignored. If the tool doesn't support that, it's probably useless and should be thrown away. In the case of a compiler, you may not be able to throw away the tool itself, and may have to settle for just globally disabling that warning.
1. Most often something like lint, but some compilers can do the same.

Why does C++ allow missing return value? [duplicate]

This question already has answers here:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Closed 6 years ago.
If a function is declared with a void return value (no return value), then it is an error for the function definition to return any value.
If a function is declared with a non-void return value (returns some value or object), then it is NOT an error if the function definition fails to return a value. Some compilers generate missing-return-value warnings about this.
What is the reason for this? To me it would make sense to have these swapped, so that an error is generated if you fail to return a value, and you only get a warning if you try to return a value from a function that is declared with a void return type.
C++ gives you the ability to shoot yourself in the foot.
It's surprisingly difficult (if not impossible) for a compiler to tell if a function returns something appropriate on all control paths.
So, like the burden of having to initialise variables before use, it leaves it to the programmer.
The behaviour on not explicitly returning a value from a non-void function is undefined.

Why does a function returning `int` in C++ compile even if I didn't include the return statement? [duplicate]

This question already has answers here:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Closed 7 years ago.
Why does a C++ function that returns a numeric value such as int or double work just fine if I don't include the return statement?
Should I avoid that even though the program runs fine?
Thanks
It could "work fine" for two reasons:
Your function is not main(), in which case flowing off the end with no return statement causes undefined behaviour1, and it just happens that it seems to work fine. But it doesn't really.
The function is main(), which has an implicit return 0;.
For case 1, you should consider your code badly broken and fix it.
1 ยง6.3.3 [stmt.return] in the C++11 standard

What does (void)variableName mean [duplicate]

This question already has answers here:
Why cast an unused function parameter value to void?
(2 answers)
Closed 8 years ago.
I saw following code few times
void func(SomeTypeEGInt varname) {
(void)varname;
}
I wish to know what it means and why people implement such functions.
It tell the compiler that those variables are unused. It is used to prevent the warnings which you will get.
The (void)varname; pattern is typically used to silence compiler warning about unused arguments. So this example is actually an empty function which does nothing.