Nameless void pointer as parameter? - c++

This is the signature of a function in VirtualBox:
static DECLCALLBACK(void) svcCall (void *,
VBOXHGCMCALLHANDLE callHandle,
uint32_t u32ClientID,
void *pvClient,
uint32_t u32Function,
uint32_t cParms,
VBOXHGCMSVCPARM paParms[])
I don't know what the first parameter, void * is supposed to mean and how one would work with it.
What is its use?

It's probably not used in that implementation. Leaving it without a name avoids the compiler warning
unused parameter xyz
To know what this parameter means you should have a look at the callback functions declaration, rather than an arbitrary implementation.

what the first parameter, void * is supposed to mean
It means that the first parameter is of type void*. Not giving the parameter a name implies that the parameter is unused on purpose.
how one would work with it
One doesn't, becase one can't work with it.
What is its use?
If you want a definitive answer, you should ask the VirtualBox developer that wrote it. I'm not them, but I can give you a guess:
Perhaps, the function is used as a callback. And the callback is specified to have a void pointer as the first parameter. But this particular callback does not need the value of the void pointer.

Related

What does void*(*void)(void*) stands for?

And what's the difference between
void*(*void)(void*)
and
void*(*voi)(void*)
and when to use it?
well void*(void)(void) doesn't compile, because it tries to declare a function that returns a void* named void and you can't use keywords of the language as names.
you can't declare an int named void either (int void=5; doesn't work of course).
Now voi is a valid identifier and you can name a function voi.
Trying to get at what you're actually asking the difference between.
void(*name)(void); and void(name)(void); is that one declares a function and the other declares a function pointer.
In fact the brackets on the second example don't do anything. void(name)(void); is the same as void name(void);.
However when declaring function pointers the brackets are needed or the * will bind left to the return type.
Anyway, I'm not sure what exactly you're asking, so you're best off searching the site for other questions regarding function pointers. Like this one that also explains why using void(name)(void) can be different from void(name)().

passing variable without a name to function

I saw a function written in the following manner:
retVal PreProcessor::TxSlotCB(void *a_pClass, PRE_PARAMS &/*commonParam*/)
{
some code
}
struct PRE_PARAMS
{
uint32_t param1;
uint32_t param2;
};
what is happening in the second parameter? how can it be empty? and is there any way to access to it?
In C++, a formal parameter can be given but anonymous. This means that the actual argument should be given but is not used in the called function.
So it should be given in calling context, it is transmitted, but the called function cannot and does not use it. And the compiler won't give any warnings.
You cannot access it in the body of the function. If you need to access it, change the declaration of the formal parameter to give it some name.
This means that parameter of type PRE_PARAM is not used by this function currently.
So, what happens is when you design a function you decide on the parameters this function would take to fulfill it's purpose.
But later you find that this parameter is not of much significance to this function. However, removing this parameter from function declaration is a tedious job as you have to check for all calls to this function and make sure they confirm to that change.
So, a better way is to not provide the name for argument in function's definition making that parameter redundant.

Forcing a call to constant version member function

I need a way to force calling a constant version of member function. I'm trying to make it efficient as possible. Any good solutions?
EDIT: looking for a generic function
Make a reference to a const Type, or a pointer to a const Type.
You can do that in a fancy way, but at the end of the day, that's the only way.
Though really I don't see what the purpose of this is. Seems like it's not worth the effort.
Also, I'm not quite sure I understand what you mean by a generic function. That needs some clarification, but if I understand correctly, you want to take a pointer/value/reference and pass it to a function and have it magically call a const method? That would only work if polymorphism or at least a method of the same name (templating) could be used.
It automatically calls the const or non-const version depending on which parameter is passed. You can only cast from const to non-const. That may seem silly, but it is the way of the world.
For instance, you could normally do this:
void function(char foo);
void function(int foo);
function(4);
The compiler will "decide for you" which one to call, you can make it explicit by doing
int x;
function((int)x);
function((char)x);
However there is no equivalent for explicitly passing an overloaded const version.
Of course, you can simply only define a const version, and parameters will simply be passed as const.

Disabling "bad function cast" warning

I'm receiving the following warning:
warning: converting from 'void (MyClass::*)(byte)' to 'void (*)(byte)'
This is because I need to pass as argument a member function instead of an ordinary function. But the program is running correctly.
I'd like to disable this warning (Wno-bad-function-cast doesn't work for C++) or to implement a different way to pass a member function.
No. Take this warning seriously. You should rather change your code to handle this scenario.
Pointer to member function(void (MyClass::*)(byte)) and normal function pointer (void (*)(byte)) are entirely different. See this link. You cannot cast them just like that. It results in undefined behavior or crash.
See here, how they are different:
void foo (byte); // normal function
struct MyClass {
void foo (byte); // member function
}
Now you may feel that, foo(byte) and MyClass::foo(byte) have same signature, then why their function pointers are NOT same. It's because, MyClass::foo(byte) is internally resolved somewhat as,
void foo(MyClass* const this, byte);
Now you can smell the difference between them.
Declare pointer to member function as,
void (MyClass::*ptr)(byte) = &MyClass::foo;
You have to use this ptr with the object of MyClass, such as:
MyClass obj;
obj.*ptr('a');
You can't pass a function that takes two arguments to a place that expects a function that takes one. Can't be done, forget about it, period, end of story. The caller passes one argument to your function. It doesn't know about the second argument, it doesn't pass it to your function, you can't make it do what you want however hard you try.
For the very same reason you can't pass a non-static member function where a regular function is expected. A member function needs an object to operate on. Whatever code calls your function doesn't know about the object, there's no way to pass it the object, and there's no way to make it use the right calling sequence that takes the object into account.
Interfaces that take user's functions, without taking additional data that the user might want to pass to his function, are inherently evil. Look at the qsort() function from the C standard library. That's an example of an evil interface. Suppose you want to sort an array of string according to some collation scheme defined externally. But all it accepts is a comparison function that takes two values. How do you pass that collation scheme to your comparison function? You can't, and so if you want it working, you must use an evil global variable, with all the strings attached to it.
That's why C++ has moved away from passing function pointers around, and towards function objects. Function objects can encapsulate whatever data you want.
Also, this may be helpful
union FuncPtr
{
void (* func)(MyClass* ptr, byte);
void (MyClass::* mem_func)(byte);
};

How can I cast or convert boost bind to C function pointer?

Suppose I have this:
void func(WCHAR* pythonStatement) {
// Do something with pythonStatement
}
And I need to convert it to void function(void) like this:
bind(func, TEXT("console.write('test')"))
Now I have struct like this:
typedef void (__cdecl * PFUNCPLUGINCMD)();
struct FuncItem {
PFUNCPLUGINCMD pFunc;
// ...
};
How can I set the pFunc of my struct to bind(func, "something")? Bind returns lambda_functor not a function pointer, so how can I cast this functor to function pointer?
Thanks.
Ended up using the wrapping "solution" (GitHub)
I think that you can't, unless you make the resulting lamba_functor a global variable.
In that case, you could declare a function that invokes it:
void uglyWorkaround() {
globalLambdaFunctor();
}
and set pFunc to uglyWorkaround().
EDIT
Just a sidenote: if you are binding static text to the function call, you may completely omit bind() call and write just:
void wrapper() {
func(TEXT("console.write('test')"));
}
and set pFunc to wrapper().
Bind returns lambda_functor not a function pointer, so how can I cast this functor to function pointer?
I don't think you can. However, off the top of my head, I can think of several alternatives:
Use boost::function<void()> (or std::function() if your compiler supports TR1 or C++11) instead of void (*)().
It has the ability to bind to just about anything with a somewhat compatible signature.
Put the whole code into a template, make PFUNCPLUGINCMD a template parameter, and let function template argument deduction figure out the exact type.
That's a variation on the former, actually, where you would use the result of bind() directly instead of having boost::function abstract away the gory details.
Create a wrapper that calls the functor returned by boost::bind().
A function template might help to let the compiler figure out the exact types and generate a suitable function, although I haven't tried to do that. However, since you cannot use the result of bind() as a template argument, but need to have give the function access to it nevertheless, you will need a global variable for this. (The ability to avoid this is one of the main advantages of function objects, a very versatile of which is std::function.)
Extend your PFUNCPLUGINCMD callback type to support a user-provided parameter. For C callbacks, this usually is a void*. However, if you pass the address of the object returned by bind() to your callback, you would need to convert it into a pointer to the correct type - which, AFAIK, depends on the arguments provided to bind(). In order to avoid that, you'd need to pass something that abstracts away the exact type. Again, std::function comes to the rescue.
The first idea would be the best, but it requires you to be able to change PFUNCPLUGINCMD. The last one might be best when PFUNCPLUGINCMD needs to be compatible with C, as it uses the common C callback idiom.
You can't do this, unless you want to write your own Just-In-Time compiler. Alternatively, if you control the receiving code, then you could use a boost::function<>, which will accept a variety of function types, including pointers and function objects like those produced by boost::bind.