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.
Related
This question already has answers here:
Why does C++ allow us to surround the variable name in parentheses when declaring a variable?
(2 answers)
Closed 5 years ago.
I am new to C++, I see following syntax in c++ to initialize variable.
int(i)=1;
Then, I have compiled in G++ compiler and compiler did not give any error or warning.
So, What does mean int(i)=1; in C and C++?
Also, I have tested in C, I thought, the C compiler give an error but it's also working fine.
It's basically a strange way to write
int i = 1;
Nothing to worry about.
Sometimes, parenthesis around the variable name are necessary in defintions (eg. pointer to functions), and there is no reason to prohibit them for other cases, so it's allowed without any deeper reason.
Maythe the author didn't like spaces (such people exist).
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.
This question already has answers here:
Error: Label used but not defined when using && operator [closed]
(3 answers)
Closed 8 years ago.
Can anyone explain how the following code creates a label?
char memory[] = "hello";
&&memory[0];
error: label 'memory' used but not defined
&&memory[0];
That's not valid C++, thus a conforming extension can assign any semantics one could want to it.
It so happens, that &&label is the GNU folks' way of taking the address of a label for computed goto's, a GNU extension.
That's it.
Reference: https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html
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.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Address of register variable
I know that getting address of register variable is not allowed in c . But why this code is getting compiled in c++ and not in c.
int main()
{
register int a;
printf("%u\n",&a);
}
The keyword register is only a hint to the compiler. In fact, most compilers today ignore it as they contain advanced code to pick the best register variable candidates anyway.
Whenever you take the address of a variable, it is typically placed on the stack, despite the fact that you have used the register keyword.