I am begginer in C++ and I have the following template class:
template<typename... extensions>
class SomeServiceProxy
{
public:
void DoSomething() {std::cout<<"\ndoing sth\n";};
};
With the following pointer to function using "using" keyword
template<typename... extensions>
using DoSomethingFnPtr = void (SomeServiceProxy<extensions...>::*)();
And I am trying to put the method in a map and call it:
std::map<int, void*> myMap;
int main()
{
SomeServiceProxy<int> proxy;
DoSomethingFnPtr<int> memfnptr = &SomeServiceProxy<int>::DoSomething;
proxy.DoSomething();
(proxy.*memfnptr)(); //works
myMap[0]= &memfnptr;
auto it=myMap.find(0);
if(it != myMap.end() ){
SomeServiceProxy<int> proxy2;
(proxy2.&(it->second))(); //error
}
std::cout<<endl;
return 0;
}
What I get is : error: expected unqualified-id before ‘&’ token
Q: Is there any way to call the method using the map?
myMap[0]= &memfnptr;already makes no sense. You are not storing the member function pointer, you are storing a pointer to the member function pointer. In any case, if you are storing just avoid*(or a member function pointer equivalent, since they cannot be cast tovoid*) with erased type in the map, then you need to cast it back to the original type before you can call it. How do you intend to use this map? Why is the typevoid*?std::map<int, std::function<void()>> myMap;and then storethis-capturing lambdas which call the member function in there. (But I am making some assumptions here. You should show the intended usage of the map.)*and()sprinkled around: godbolt.org/z/f9vKGzjvx. Still, as user17732522 said, it doesn't make much sense to try that, you should show what are you trying to do instead of attempted solution.... extensionsand you arent using them I suggest to start without them. Once it works without extensions you can still add them back in(proxy2.*(*reinterpret_cast<DoSomethingFnPtr<int>*>(it->second)))(). (You can tell from how incomprehensible that is that you're heading down a bad path to whatever it is that you want to accomplish.)