Variadic template with member function pointer as parameter

I watched a few examples on the Internet, and I do not understand why this does not compile. what I'm trying to do is passed by the say class Object member function, to a class that has a vector of specified objects, and has a function with argument templates as parameters ... Example:

template <typename ...Args_t> bool objectsDo(bool (Object::*func)(Args_t...), Args_t&&... args) { for (int i = 0 ; i < OBJECTS ; ++i) { if (!m_objects[i]->*func(std::forward<Args_t>(args)...)) { return false; } } return true; } 

but every function that I try to execute, even without parameters, that I get:

 error: no instance of function template "objectsDo" matches the argument list argument types are: (bool ()) objectsDo(&Object::close); 

where is my use:

  objectsDo(&Object::close); 

EDIT: as Colombo suggested, I now send the address of the function, but still I get errors when sending with parameters, for example:

  error: no instance of function template "objectsDo" matches the argument list argument types are: (bool (Object::*)(int, char), int, char) 
+5
source share
1 answer

I assume that you are calling the function as follows:

 int main() { int i; char c; objectsDo(&Object::close, i, c); } 

The problem is that the template arguments are output as follows:

 template <typename ...Args_t> bool objectsDo(bool (Object::*func)(Args_t...), Args_t&&... args) 

Args_t is output as int, char for the first parameter and int&, char& for the second. This is because of the inner workings of universal links: it works with dropping links.

Use another package parameter for the following parameters:

 template <typename ...Args_t, typename... Args> bool objectsDo(bool (Object::*func)(Args_t...), Args&&... args) { /* […] */ } 

Or make the trailing parameter an inactive context:

 template <typename T> struct identity {using type = T;}; template <typename T> using identity_t = typename identity<T>::type; template <typename ...Args_t> bool objectsDo(bool (Object::*func)(Args_t...), identity_t<Args_t>... args){ // […] } 
+6
source

Source: https://habr.com/ru/post/1206517/


All Articles