0
votes

I am facing compilation error on passing 1. unique ptr to template class member function 2. contents of unique pointer to template class member function

I am trying to push data inside a vector . The data is of template type. The unique pointer here is used here to create a memory on heap everytime data comes and then store that data into the vector. Even copying of pointer pointing to the new location can also be pushed inside the vector. I have tried both 1. Catching the data using T item;//below mentioned code 2. Catching the unique pointer with reference using std::unique_ptr & item; Sample code is kind of snippet for a long code

#include <stdio.h>
#include<iostream>
#include<vector>
#include <mutex>
#include <condition_variable>
#include <boost/any.hpp>
using namespace std;
namespace amb
{

template <typename T>
class Queue
{
public:
        Queue(bool unique = false, bool blocking = false)
                :mUnique(unique), mBlocking(blocking)
        {
        }
        virtual ~Queue()
        {
        }
        int count()
        {
                std::lock_guard<std::mutex> lock(mutex);
                return mQueue.size();
        }
        T pop()
        {
                std::unique_lock<std::mutex> lock(mutex);
                uint16_t i =0;
                if(mBlocking)
                {
                        if(!mQueue.size())
                        {
                                cond.wait(lock);
                        }
                }
                if(!mQueue.size())
                {
                        throw std::runtime_error("nothing in queue");
                }
                auto itr = mQueue.begin();
                T item = *itr;
                mQueue.erase(itr);
                return item;
        }

        virtual void append(T  item)
        {
            std::lock_guard<std::mutex> lock(mutex);
                        mQueue.push_back(item);
        }
        void remove(T item)
        {
                std::lock_guard<std::mutex> lock(mutex);

                removeOne(&mQueue, item);
        }
        std::vector<T> get_Val()
        {
                return mQueue;
        }
    private:
        bool mBlocking;
        bool mUnique;
        std::mutex mutex;
        std::condition_variable cond;
        std::vector<T> mQueue;
};
}

class AbstractPropertyType
{
public:
 AbstractPropertyType(){}

 virtual ~AbstractPropertyType()
    {

    }

 virtual AbstractPropertyType* copy() = 0;
 boost::any anyValue()
    {
        return mValue;
    }
 protected:

    boost::any mValue;
};

template <typename T>
class BasicPropertyType: public AbstractPropertyType
{
public:
    BasicPropertyType(): AbstractPropertyType("")
    {
        mValue = T();
    }
    AbstractPropertyType* copy()
    {
        return new BasicPropertyType<T>(*this);
    }
};
amb::Queue<AbstractPropertyType*> prop;
void updateProperty(AbstractPropertyType * val)
{
    std::unique_ptr<AbstractPropertyType> temp_data =  std::unique_ptr<AbstractPropertyType>(val->copy());
    prop.append(temp_data);
}
int main() {
    //code
    uint8_t x = 10;
    uint8_t * test_val = &x;
    boost::any to_check = (uint8_t *)test_val;
    AbstractPropertyType * value = new BasicPropertyType<uint32_t>;
    updateProperty(value);
}

Main moto is 1. Either push the contents of Template data into the vector 2. Pointer pointing to the

Compilation error : prog.cpp: In function 'void updateProperty(AbstractPropertyType*)': prog.cpp:109:26: error: no matching function for call to 'amb::Queue::append(std::unique_ptr&)' prop.append(temp_data); ^

1
The error is obvious. Raw pointer is not a smart pointer.wilx
@wilx , Sorry I dint get it , can you pl elaborate ? Which raw pointer you are talking aboutVBurman

1 Answers

0
votes
amb::Queue<AbstractPropertyType*> prop;

So, prop.append() takes an argument of type AbstractPropertyType*, but you are sending temp_data which is of type std::unique_ptr<AbstractPropertyType>. Since the append function expects a raw pointer, you should pass the raw pointer inside it. There is no implicit conversion from unique_ptr<T> to T*.

void updateProperty(AbstractPropertyType * val)
{
    std::unique_ptr<AbstractPropertyType> temp_data =  std::unique_ptr<AbstractPropertyType>(val->copy());
    prop.append(temp_data.get());
}