6

I've watched several examples online and I don't understand why this doesn't compile.. what I'm trying to do is passed a member function of say class Object, to a class that has a vector of said Objects, and have a function with templated arguments as parameters be called... 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 I try, even a parameterless one I get:

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

where my usage is:

            objectsDo(&Object::close);

EDIT: as suggested by Columbo, I am now sending the address to the function, but still I get errors when sending with parameters, such as :

  error: no instance of function template "objectsDo" matches the argument list
            argument types are: (bool (Object::*)(int, char), int, char)
8
  • What compiler are you using? And you're missing a & before Object::close Commented Nov 9, 2014 at 11:57
  • I believe in c++ you don't need that & anymore and it defaults to it, hmm using icc actually but I've done forwarding of variadic templates to c'tors before, I believe it's something with the member fucntion. Commented Nov 9, 2014 at 11:59
  • It's not about what you believe. Did you try it with an ampersand? Compiles fine for me Commented Nov 9, 2014 at 12:02
  • You're right, editted. Commented Nov 9, 2014 at 12:05
  • 1
    Ahh, got it. Give me a minute. Commented Nov 9, 2014 at 12:11

1 Answer 1

9

I assume you call the function like this:

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

The problem is that the template arguments are deduced inconsequently:

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

Args_t is deduced as int, char for the first parameter and int&, char& for the second. That is because of the internal working of universal references: It works with reference collapsing.

Use another parameter-pack for the trailing parameters :

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

Or make the trailing parameter a 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){
    // […]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.