C++ 以成员函数指针为参数的可变模板
我在网上看了几个例子,我不明白为什么这个不能编译。。 我要做的是把一个类对象的成员函数传递给一个类,这个类有一个对象向量,并且有一个函数,它的参数是模板参数,可以被调用。。。 例如: 我的用法是:C++ 以成员函数指针为参数的可变模板,c++,templates,variadic-templates,C++,Templates,Variadic Templates,我在网上看了几个例子,我不明白为什么这个不能编译。。 我要做的是把一个类对象的成员函数传递给一个类,这个类有一个对象向量,并且有一个函数,它的参数是模板参数,可以被调用。。。 例如: 我的用法是: objectsDo(&Object::close); 编辑: 正如Columbo所建议的,我现在正在向函数发送地址,但在使用参数发送时仍然会出现错误,例如: error: no instance of function template "objectsDo" m
objectsDo(&Object::close);
编辑:
正如Columbo所建议的,我现在正在向函数发送地址,但在使用参数发送时仍然会出现错误,例如:
error: no instance of function template "objectsDo" matches the argument list
argument types are: (bool (Object::*)(int, char), int, char)
我假设您这样调用函数:
int main()
{
int i; char c;
objectsDo(&Object::close, i, c);
}
问题在于模板参数的推导不合理:
template <typename ...Args_t>
bool objectsDo(bool (Object::*func)(Args_t...), Args_t&&... args)
或者将尾部参数设置为非推断上下文:
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){
// […]
}
模板结构标识{using type=T;};
模板
使用identity\u t=typename identity::type;
模板
bool objectsDo(bool(Object::*func)(Args\u t…,identity\u t…Args){
// […]
}
您使用的编译器是什么?在代码>对象之前:你错过了<代码>和代码>:关闭/<代码>我相信C++你不需要它,而且它默认了它,实际上使用了ICC,但是我已经把可变模板转发给c'Tror之前,我相信它和成员函数有关。它不是关于你相信什么。你试过用符号吗?你说得对,伊迪特。啊,明白了。给我一分钟。
template <typename ...Args_t>
bool objectsDo(bool (Object::*func)(Args_t...), Args_t&&... args)
template <typename ...Args_t, typename... Args>
bool objectsDo(bool (Object::*func)(Args_t...), Args&&... args)
{ /* […] */ }
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){
// […]
}