Member function pointers to static function pointers?

Hi, all!

I was trying to take advantage of variadic template arguments introduced in C++11. Here's my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
template<class Return, class... Args>
class Delegate{
    /// Type definitions:
    typedef Return (*function_t)(Args...);
public:
	/// Constructors & destructors:
	Delegate(function_t function) : _m_pfPointer(function){

	}
	/// Member functions:
	template<class T>
	Delegate& operator=(T function){
            _m_pfPointer = reinterpret_cast<Return (*)(Args...)>(function);
            return *this;
	}
private:
	/// Member data:
	function_t _m_pfPointer;
};

bool function(bool){
    return false;
}

class Class{
public:
    /// Member functions:
    bool function(bool){
        return false;
    }
};

int main(int argc, char* argv[]){
	Delegate<bool, bool> __delegate = &function;
	__delegate = &Class::function;
	return 0;
}


When using GCC in Code::Blocks, it warns me about conversion process in member function Delegate::operator=:
warning: converting from 'bool (Class::*)(bool)' to 'bool (*)(bool)'


So, how can I cast member function pointer to static member function pointers?
Last edited on
closed account (zb0S216C)
Henri Korpela wrote:
typedef Return (*function_t)(Args...);
function_t is a pointer to a non-member function or a static member function. So when you assign the address of Class::function to __delegate, it attempts to convert _m_pfPointer to a pointer-to-a-member-function. In other words, it attempts to convert a bool(*)(bool) to a bool(Class:*)(bool), which it cannot do.

1 solution would be to create a separate class to handle a pointer-to-a-member-function. For instance:

1
2
3
4
5
6
template <typename Ret, typename Class>
class PointerToAMemberFunction
{
    private:
        Ret(Class:: *Pointer__)(...);
};


Wazzak
Last edited on
Topic archived. No new replies allowed.