Tuesday, 15 May 2012

c++ - Variadic template with member function pointer as parameter -



c++ - Variadic template with member function pointer as parameter -

i've watched several examples online , don't understand why doesn't compile.. i'm trying passed fellow member function of class object, class has vector of said objects, , have function templated arguments parameters called... example:

template <typename ...args_t> bool objectsdo(bool (object::*func)(args_t...), args_t&&... args) { (int = 0 ; < objects ; ++i) { if (!m_objects[i]->*func(std::forward<args_t>(args)...)) { homecoming false; } } homecoming true; }

but every function try, parameterless 1 get:

error: no instance of function template "objectsdo" matches argument list argument types are: (bool ()) objectsdo(&object::close);

where usage is:

objectsdo(&object::close);

edit: suggested columbo, sending address function, still errors when sending parameters, such :

error: no instance of function template "objectsdo" matches argument list argument types are: (bool (object::*)(int, char), int, char)

i assume phone call function this:

int main() { int i; char c; objectsdo(&object::close, i, c); }

the problem template arguments deduced inconsequently:

template <typename ...args_t> bool objectsdo(bool (object::*func)(args_t...), args_t&&... args)

args_t deduced int, char first parameter , int&, char& second. because of internal working of universal references: works reference collapsing.

use parameter-pack trailing parameters :

template <typename ...args_t, typename... args> bool objectsdo(bool (object::*func)(args_t...), args&&... args) { /* […] */ }

or create trailing parameter non-deduced 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){ // […] }

c++ templates variadic-templates

No comments:

Post a Comment