+ 2
What is the purpose of (void*) before a function call, while function return a pointer to void?
foo( (void *)request.data(), "Hello", 5); I found something about it: A cast to void can have semantic effect in one case: where a value is an operand of the comma operator and overrides the comma operator, a cast to void will suppress it. https://stackoverflow.com/questions/13954517/use-of-void-before-a-function-call Is that true?? Does anyone have a comment?
1 Respuesta
+ 4
void and void* are a bit different though.
void* is a pointer to an unknown type.
void literally means nothing.
void* is often used where you only care about the address and not the actual type, especially in C. ( maybe because of the lack of overloading/templates? )
See functions like malloc, memset or memcpy.
By casting something to a void* you're saying something along the lines of:
"Hey, forget about your type, no one cares."
I doubt you'll come across void* alot in C++ unless you're dealing with low level memory stuff and even then... maybe.
Also an addendum to the link you posted about casting to void;
a useful case is with the ternary operator, as it requires the types to be either the same or at least be convertable to one another.
If you have something like:
void a(){ ... }
int b(){ ... }
and you want to make a call to a or b based on a condition but don't care about the return value.
Simply doing 'cc ? a() : b();' would fail because void and int are different.
But 'cc ? a() : (void)b();' compiles fine because you're just ignoring the return type of b by doing that.