简体   繁体   English

使用成员函数指针作为参数的可变参数模板

[英]Variadic template with member function pointer as parameter

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: 我在线观看了几个例子,我不明白为什么这不能编译..我想要做的是将类Object的成员函数传递给一个具有所述Objects的向量的类,并且具有模板化参数的函数作为参数被调用...示例:

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 : 编辑:正如Columbo所建议的,我现在正在向函数发送地址,但在使用参数发送时仍然会出错,例如:

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

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. Args_t推导为int, char第一个参数为int&, char& ,第二个为int&, char& 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){
    // […]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++:具有可变参数模板的成员函数指针参数 - C++: Member function pointer parameter with variadic template GCC失败了可变参数模板和指向成员函数的指针 - GCC failed with variadic template and pointer to member function 模板参数 - 具有可变参数的函数指针 - Template parameter - function pointer with variadic arguments 指向struct / class的递归成员的指针的变量序列作为模板参数 - Variadic sequence of pointer to recursive member of struct/class as template parameter 指向可变参数函数模板的指针 - Pointer to variadic function template 可变参数模板指针,指向成员函数的声明/使用问题 - Variadic template pointer to member function declaration/usage issues 具有静态绑定的成员函数指针的可变参数模板的多个专业化? - multiple specializations of a variadic template with a statically bound member function pointer? 使用可变参数模板的成员函数指针包装器(gcc,clang) - Member function pointer wrapper using variadic template (gcc, clang) 使用可变参数模板参数传递成员函数指针是模棱两可的 - Passing Member Function pointer using variadic template arguments is ambiguous 可变参数模板中的函数参数 - Function parameter in variadic template
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM