有没有一种方法来传递模板参数的函数时,对象类型是一个模板参数的对象呢? [英] Is there a way to pass template arguments to a function on an object when the object type is a template argument?
本文介绍了有没有一种方法来传递模板参数的函数时,对象类型是一个模板参数的对象呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要说明:
struct MyFunc {
template <size_t N>
void doIt() {
cout << N << endl;
}
};
template <typename Func>
struct Pass123ToTemplateFunc {
static void pass(Func f) {
f.doIt<123>(); // <-- Error on compile; is there a way to express this?
}
};
int main() {
Pass123ToTemplateFunc<MyFunc>::pass(MyFunc());
return 0;
}
这是pretty多少纯粹是语法的好奇心;有没有在语言前preSS这种方式没有传递参数给 doIt方法
函数本身?如果不是,没什么大不了的,我已经清楚知道我的方法可以优雅地解决它,所以没有必要提供替代解决方案。 (我会接受不作为回答,换句话说,如果这是事实。:-P)
This is pretty much purely a syntax curiosity; is there a way in the language to express this without passing arguments to the doIt
function itself? If not, it's no big deal and I'm already well aware of ways I can gracefully work around it, so no need to provide alternative solutions. (I'll accept "no" as an answer, in other words, if that's the truth. :-P)
推荐答案
您必须告诉编译器的doIt
将是一个模板:
You have to tell the compiler that doIt
will be a template:
f.template doIt<123>();
这篇关于有没有一种方法来传递模板参数的函数时,对象类型是一个模板参数的对象呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文