Search code examples
c++stlcircular-buffer

c++ template specialization in circular queue


Hi i am tryng to develop a circular queue, but this implementation works fine for basics type like int float and so on,

template<typename R, size_t aSize=200>
class CircularQueue{
public:

    explicit CircularQueue(const R & aElement);


    ~CircularQueue()
    {

    }

    bool push(const R & aElement);

    bool pop(R & aElement);

    bool isEmpty() const    ;

    bool isFull() const     ;
private:
    CircularQueue(){}
    CircularQueue(const CircularQueue &);
    const CircularQueue & operator=(const CircularQueue &);
    int incrementar(int  ) const;
        static const size_t capacity=aSize+1;
        R array[capacity];
        volatile int tail;
        volatile int head;

};

but when i try to speciallized this to a custom type the compiler tells that i have to call the especif constructor: Especif class

    class PutMessage: public  IMetodo, Sujeto<PutMessage>
    {
    public:
       explicit PutMessage(Servant * aServant = 0,Mensaje * aMensaje=0, Observer<PutMessage> * aFuture=0);
        virtual ~PutMessage();
    bool guard() const;
    int getResult() const ;
    void call();

    Mensaje * getMensaje() const;

    Servant * getServant() const;

    bool hasFuture() const;
    private:
    PutMessage();
    Servant * mServant;
    Mensaje * mMensaje;
    int mResult;
    bool mHasFuture;
};
}

the call to the circular queue:

CircularQueue<PutMessage,32> aCircular(*aMessageQueue);

Do i have to reimplement the class to a semispecialization class??


Solution

  • The problem is caused by this data member:

    R array[capacity];
    

    An array already contains all its elements, and so a call to R's constructor is required for each one. Why not use std::vector instead?