简体   繁体   English

将模板类传递给模板函数

[英]passing a template class to a template function

I am forgetting the syntax in this moment. 我现在忘记了语法。 Can someone please help? 有人可以帮忙吗? Basically below is what I am trying to do. 基本上,下面是我想做的事情。 I don't mind to set it up to take two template arguments if needed meaning runSchedule<SchedT, TaskT>() if this is easier. 如果需要的话,我不介意将其设置为接受两个模板参数,这意味着runSchedule<SchedT, TaskT>() Also if you could comment on how to get the using alias to work for scheduler_type and scheduler_type::task_type to be recognized as types within the function. 另外,如果您可以评论如何获取使用别名,以使scheduler_typescheduler_type::task_type被识别为函数中的类型。

#include <iostream>                                                                                                                                                                                                                 

class TestTask  {                                                                                                                                                                                                                   
public:                                                                                                                                                                                                                             
    int x;                                                                                                                                                                                                                          
};                                                                                                                                                                                                                                  

template <typename TaskT>                                                                                                                                                                                                           
class TestScheduler {                                                                                                                                                                                                               
public:                                                                                                                                                                                                                             
    typedef  TaskT task_type;                                                                                                                                                                                                       
    int y;                                                                                                                                                                                                                          
};                                                                                                                                                                                                                                  

template <template<class> class SchedT>                                                                                                                                                                                             
void runSchedule()  {                                                                                                                                                                                                               
    typedef  SchedT scheduler_type;                                                                                                                                                                                                
    scheduler_type sched;                                                                                                                                                                                                           
    scheduler_type::task_type task;                                                                                                                                                                                                 
}                                                                                                                                                                                                                                   

int main() {                                                                                                                                                                                                                        
    runSchedule<TestScheduler<TestTask> >();                                                                                                                                                                                        
}      

You don't need a template template-parameter for what you're trying to do. 您无需为要执行的操作提供模板template-parameter。

template < class SchedT>
void runSchedule()  {
    typedef SchedT scheduler_type;  // <-- typedef syntax was backwards
    scheduler_type sched;
    typename scheduler_type::task_type task;
    // ^^^^^ need typename keyword when referring to nested dependent type
}

The template<class> class SchedT in your template function argumets? 您的template函数argumets中的template<class> class SchedT SchedT?

Replace with class SchedT . 替换为class SchedT

The earlier syntax is for passing the template , the later a class generated by the template . 早期的语法是路过的template ,后来由生成的类template

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM