bjackfly
bjackfly

Reputation: 3336

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. 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.

#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> >();                                                                                                                                                                                        
}      

Upvotes: 1

Views: 76

Answers (2)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275405

The template<class> class SchedT in your template function argumets?

Replace with class SchedT.

The earlier syntax is for passing the template, the later a class generated by the template.

Upvotes: 2

Praetorian
Praetorian

Reputation: 109119

You don't need a template template-parameter for what you're trying to do.

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
}

Upvotes: 4

Related Questions