invalid use of non-static member function - c++

In my Qt application I wish to be able to add a tab to a non-static QTabWidget when the user presses a shortcut. Normally I would implement it like this:
File.addAction("Settings", openSettings, Qt::ALT + Qt::Key_S);
where File is a QTabWidget and openSettings is a static method. The only issue with that is that it only works for static methods, and the issue with that is that I can't access a non-static variable in a static method. As such I figured that since Qt asks for the function to be a static function I can instantiate a static std::function<> object as such:
static std::function<void(void)> openSettings_ = []() { openSettings; };
and call it as such
File.addAction("Settings", openSettings_, Qt::ALT + Qt::Key_S);
The issue with this is that it generates the error:
Error: invalid use of non-static member function 'void window::openSettings()'
My reasoning for this is that I am familiar with C and assumed that what Qt calls a functor is almost the same as a function pointer that pretty much is an object. As such, I assumed that if I were to instantiate a static object of type std::function that pointed to / executed a non-static function I would get around this issue, which is clearly not the case. How would I go about doing this, seeing as my current thought process is wrong?

First, the immediate error is raised because you're not actually calling the function. You must call it: openSettings();.
However, this won't work. openSettings is non-static member function. All such normal member functions take an implicit this pointer to the object on which they're being invoked. This means that one cannot directly invoke the openSettings function without an object on which to invoke it. But this is not captured by the lambda you've written, meaning there's no such object.
This can be fixed by capturing this in the lambda, such as auto openSettings_ = [this]() { this->openSettings(); };
But on the other hand, this function is acting like a slot. You should attach the signal you're interested in directly to the signal using the standard signal/slot syntax, rather than writing the separate functor. That would be something like this.
File.addAction("Settings", this, &(decltype(*this))::openSettings, Qt::ALT + Qt::Key_S);
(Note that I'm using decltype because I'm not sure what type *this is. You can substitute with the name of the class.)

Related

C++: closure to pass member function as normal function pointer

I'm trying to call a member function of an external library which takes a function pointer as a parameter:
Timer::every(unsigned long period, void (*callback)(void));
But unfortunately the parameter I want to pass is a member function:
void MyClass::the_method_i_want_to_pass(void);
Since I'm programming for the ATMega under Arduino (AVR) there is just limited support of c++11. My first approach raises a type error:
void MyClass::the_method_i_want_to_pass() {...}
MyClass::MyClass() {
// constructor
Timer *timer = new Timer();
timer->every(500, [this](){this->the_method_i_want_to_pass();})
}
Compiler Output:
warning: warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]
error: no matching function for call to ‘Timer::every(int, MyClass::MyClass()::__lambda0)’
Are there other/better solutions?
Concerning my current approach: (How) is it possible to pass a reference to a lambda when a function pointer is required?
How can I find out if Arduino/AVR supports these lambdas (see "warning")?
Your basic problem is your Timer library is poorky written: it should take void(*)(void*), void* at the least.
Without a pvoid or equivalent, you cannot pass any state other than the address in execution code to run the procedure at. As a method also rewuires a this pointer, you are out of luck.
Now, if your instance of MyClass is a singleton, you can get this from somewhere else.
Failing that, you need to make your own global state that lets you map from a particular callback to some state. If you have a limited number of MyClass and other consumers of Timer, you can have a few fixed functiins, and have them store their extra state globally.
This is all a hack. What follows is worse.
Write a dynamic library with some global state, and a void() interface. When you add a callback, duplicate that dynamic library, modify its global state at runtime, write it out as a differently named library, load it, and pass the pure callback function to your Timer class.
Or do the equvalent without a library by manually writing machine code and marking pages as execuable.
These are all poor solutions. Which leads me to a good one: find a better Timer. If they screwed up something that simple, the rest of the library is probably bad as well.

Is there any way to change the scope of a callback without changing the paramaters?

I am using SDL2_mixer library, but I believe that the question should hold for the general case also.
Currently, a function that I would like to use, Mix_HookMusicFinished(void (*music_finished)(void)) has a set callback to the global scope for a C style function. However, I would like to have that callback be set to a member function within my own class void CMusic::musicFinished() without having the need for a function in global scope.
Is there anyway to do this? Something like Mix_HookMusicFinished(musicFinished) would be great, but that directly has an error of argument of type "void (CMusic::*)()" is incompatible with parameter of type "void (*)()"
You need to make a "wrapper" function. However, the problem here is that you also need to be able to find the CMusic object that you want to "finish" - this is really what the crux of
argument of type ... is incompatible with ...
is all about. Since there is no way to pass a parameter to the musicFinished object, you will need some other way of "finding" the CMusic object.
If we assume there is a way to do that, then something like this would work:
class CMusic
{
...
public:
...
static void musicFinishedWrapper();
void musicFinished();
...
};
void CMusic::musicFinishedWrapper()
{
CMusic* music = getTheMusicSomehow(); // No idea how you do this - depends on your code.
music->musicFinished();
}
The reason you have to have a CMusic object is that your musicFinished expects a (hidden) this pointer argument - which is the value in music in my little function.
You could move musicFinished to your CMusic class and declare it as a static class method. static class methods aren't called on an object; they therefore don't have an implicit argument to specify the value of the this pointer, and they therefore can have the same signature as freestanding functions. You additionally can make it private to prevent anything but CMusic from using it.
However, since your musicFinished method currently works as a freestanding function and therefore probably doesn't need access to CMusic's protected or private members, and since your efforts to limit its scope presumably means that you don't want other things to call it, I personally would leave your musicFinished function as freestanding but declare it as static (or move it to an anonymous namespace, if you prefer) within the CMusic source (.cpp or .cc) file. Doing so would restrict its scope to the source file (the "compilation unit"). An advantage over a private, static class method is that it does not need to be exposed at all in a header file, so it is in some sense more private.

How to get a "simple" function pointer from a member function

I'm having a problem with function pointers and nothing I found on the net helped me to solve this problem.
I have a function from a C API which take a pointer of a void function :
extern int APIFunction(int, void (*func)(int));
I have a class with the function I would like to put when I call the API function.
class MyClass
{
public:
void myFunction(int status, otherAPi arguments...);
};
Then, I created a pointer to my member function and created a new instance of my class
typedef void (MyClass::*MyClassFunctionPointer)(int stat, otherAPi arguments...);
MyClassFunctionPointer fctPointer= &MyClass::myFunction;
LicenseSecurity instance;
I get an error when I try to call my APi function with the function pointer I created:
int stat = APIFunction(5, fctPointer ); // -> error 1
int stat = APIFunction(5, instance.*fctPointer ); // -> error 2
I got errors respectively in the first and second case:
E2034 Impossible to convert 'void (MyClass::*)(int, otherAPITypes...)' into 'void (*) (int, otherAPITypes...)'
E2342 Bad type correspondence in the parameter 'func' ('void (*)(int, otherAPITypes...)' desired, 'void(int, otherAPITypes...)' obtained)
I don't have access to the API function so I can't modify it. To summary the problem: how How to get a "simple" C function pointer to put in argument of a function from a member function of my class?
Thanks
Unfortunately, you can't. Sorry.
Ideally, your API would accept something like std::function that would allow you to wrap free functions or member functions. But if you can't modify the API, then you have no choice but to provide a free function.
You can't get a "simple" function pointer to a non-static member function because the function requires a this pointer when called. If you were to create a function pointer like that then when the function was called there would be no this pointer for it to reference.
With an ancient C API like that, you unfortunately don't have any way to do this.
What you have to do is make a static or non-member function to take the callback, and then figure out which instance of the object to call the member on. Some C APIs allow a user data to be passed to the callback, and in that case you use that to store the this pointer in question. If that's not an option you can use a global or singleton object and only allow a single such callback to be registered.
You can declare the callback as either a standalone function or as a static method of the class. The tricky part is accessing a class instance pointer inside the callback.
Ideally, a well-designed API allows you to specify a user-defined value to callbacks. That allows you to easily pass in a class instance and access it directly inside the callback. But it sounds like you are not working with such an API, so you need to use a workaround.
If you have only 1 class instance being used with the API at a time, you can store the instance pointer into a global variable, and have the callback use the global variable to access the instance.
But if you have multiple class instances being used at the same time, you are looking for a thunking solution, similar to the VCL's MakeObjectInstance() function, which allows TWndMethod-signatured class methods to be used as Win32 window procedure callbacks. Essentially, a block of executable memory is dynamically allocated, stub assembler code is written into the block, and the instance pointer and class method pointer are stored in the block as well. The block is then passed to the API as if it were a function pointer. When the API calls the "function", the stub code gets executed, which has to manipulate the call stack and CPU registers to call the stored class method pointer passing the stored instance pointer as its hidden this parameter, while preserving the semantics of other parameters, the call stack, function result, etc.
Nothing in C++ really accomplishes that kind of thunking natively. It is not difficult to implement manually, but it is not trivial either (have a look at the source code for MakeObjectInstance() in the VCL's Classes.pas source file). The hardest part is coming up with the necessary stub code that matches the semantics of your particular class method's signature.

How do you pass a non-static function as a callback?

I'm currently working with Qt and a graphics engine and during the init of the QGLWidget instance I need to pass a few function pointers to my engine.
The function looking for callbacks is:
virtual void Buffer::CreateCustom( byte* getsize, byte* makecurrent)
Qt provides a makeCurrent function however it is neither byte* nor static.
I could write a tiny wrapper function like so:
void _stdcall MakeCurrent(void)
{
QGLContext::makeCurrent();
}
But its only meant to be called from within an instance of GLWidget. I tried to create a class member wrapper function like so:
void _stdcall LEWidget::leMakeCurrent(void)
{
makeCurrent();
}
But you can only provide function pointers on static member functions. If I do that I get the following error:
error C2352: 'QGLWidget::makeCurrent' : illegal call of non-static member function. A nonstatic member reference must be relative to a specific object.
See this question, I think it is pretty much what you want to do:
How do I implement a callback in C++?
You can't. That's what std::function exists for. You need to either change your interface to use std::function, get lucky and find some kind of void* context argument, or give up.
This is because it is impossible to tell(from the compiler's POV) which this pointer should be pass into that function when that callback is called. If you really really want to pass in a pointer, you'll have to use assembly.

Moving callback function to a class causes an error!

After I moved some OpenGL code from main function to a new class I had the following error on the following row:
glutDisplayFunc(OnDisplay);
error C3867: 'Room::OnDisplay': function call missing argument list; use '&Room::OnDisplay' to create a pointer to member
What was my fault ?
glutDisplayFunc expects a void (*func)(void), but you're passing in a void (Room::*func)(void).
Since class methods receive an implicit this parameter, their pointer types are fundamentally different than regular function pointers. There's no conversion possible between them.
All you can do is make OnDisplay a static member of Room. From there you can forward the call to a member function of a concrete Room instance (since there is by design only one glut display callback and you migrated from procedural code, I presume you have only a single Room object somewhere).
glutDisplayFunc just takes pointer to the function. When moved OnDisplay to the class, you will also pass the hidden argument this to glutDisplayFunc when actually get called.
One possible solution is to make OnDisplay as a static method.