My template queue is below
template <typename T>
class LockingQueue
{
private:
std::queue<T> s_queue;
public:
void push(T const& value)
{}
T pop()
{}
};
And my template class is below
template <typename TaskData, typename TaskName>
class CommonMsg
{
public:
TaskData dataType;
TaskName taskName;
};
template <typename TaskData, typename TaskName>
using CommonMsgPtr = boost::shared_ptr<CommonMsg<TaskData, TaskName>>;
template <typename TaskData, typename TaskName>
using CommonMsgConstPtr = boost::shared_ptr<const CommonMsg<TaskData, TaskName>>;
I want to put the template class as the parameter of LockingQueue, e.g.
LockingQueue<CommonMsgConstPtr >
. I know it is wrong. what should I do?
As far as I am aware, you can do this in two ways. In both ways you need to specialize your original class template so that it gets instantiated when you pass a smart pointer. The simplest way is option 1 (see code below), but you must specify the type in the main function. A bit dirtier approach is option 2 (see code below), but the call in the main function is cleaner and closer to your original post.
Btw, why boost::shared_ptr
and not std::shared_ptr
?
#include <iostream>
#include <queue>
#include <memory>
template <typename T>
struct LockingQueue
{
std::queue<T> s_queue;
...
};
// Option 1
template < typename CM, template<typename> class SP>
struct LockingQueue<SP<CM>>
{
std::queue< SP<CM> > s_queue;
...
};
// Option 2
template < typename T, typename R, template<typename,typename> class CM, template<typename> class SP>
struct LockingQueue<SP<CM<T,R>>>
{
std::queue< SP<CM<T,R>> > s_queue;
...
};
template <typename TaskData, typename TaskName>
struct CommonMsg
{
TaskData dataType;
TaskName taskName;
};
template <typename TaskData, typename TaskName>
using CommonMsgPtr = std::shared_ptr<CommonMsg<TaskData, TaskName>>;
template <typename TaskData, typename TaskName>
using CommonMsgConstPtr = std::shared_ptr<const CommonMsg<TaskData, TaskName>>;
// Option 1 alias
template < typename CM, template<typename> class SP = std::shared_ptr>
using CommonMsgConstSPtr1 = SP< const CM >;
// Option 2 alias
template < typename TaskData = int, typename TaskName = char, template<typename,typename> class CM = CommonMsg, template<typename> class SP = std::shared_ptr>
using CommonMsgConstSPtr2 = SP< const CM<TaskData, TaskName> >;
int main()
{
LockingQueue< CommonMsgConstSPtr1< CommonMsg<int,char> > > option1;
LockingQueue< CommonMsgConstSPtr2<> > option2;
}
Online example: https://ideone.com/h6o02M