mardi 26 mai 2015

I have the following situation:

Base is a base class. T is a template that can assume any derived class of Base.

The underlying layer provide me data from Base class, that I need to convert to a specific class on the above layer (that layer where the code is written) to work on a user level.

Here is the code:

template <class T> class Access {
       std::vector<std::unique_ptr<T> getData();        
}

template <class T>
std::vector<std::unique_ptr<T> getData()
{
      /// Get data from below layer
      std::vector<std::unique_ptr<Base>> retData; 

      retData = getDataFromBelowLayer();

      /// Now I have to cast Base to T
      std::vector<std::unique_ptr<T>> retCastData;

      for (auto &item : retData)
      {
          std::unique_ptr<T> = static_cast<T>(item); <<---- NOT WORKING
          retCastData.push_back(std::move(item));    <<---- NOT WORKING
      }

      return retCastData;
}

How can I efficiently cast the vector of unique_ptr´s of Base class received to the vector of unique_ptr´s of T type as shown.

Thanks for helping.

Aucun commentaire:

Enregistrer un commentaire