c ++将unique_ptr<Base>的向量转换为unique_ptr<Derived>,其中derived是一个模板
c++ Cast a vector of unique_ptr<Base> to unique_ptr<Derived> where derived is a template
我有以下情况:
Base
是基数 class。
T
是一个模板,可以假定 Base
的任何派生 class。
底层为我提供来自 Base
class 的数据,我需要将其转换为上层(编写代码的那一层)上的特定 class在用户级别上工作。
代码如下:
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;
}
如何有效地将接收到的 Base
unique_ptr´s
class 的 vector
转换为 [=[=] unique_ptr´s
的 vector
16=] 如图所示键入。
感谢您的帮助。
这样做:
struct Base {};
template<typename T>
struct Derived : public Base {};
template <typename T>
std::vector<std::unique_ptr<T> > getData()
{
//add some checking:
static_assert(std::is_base_of<Base, T>::value, "T must be derived from Base");
std::vector<std::unique_ptr<Base> > retData;
//fill it somehow
std::vector<std::unique_ptr<T> > retCastData;
for (auto& item : retData)
{
auto t = std::unique_ptr<T>(static_cast<T*>(item.release())); //(*)
retCastData.push_back(std::move(t));
}
return retCastData;
}
int main()
{
getData<Derived<int> >(); //or some other type than "int"
}
主要的事情发生在标有(*)
的行中。这里释放了唯一指针,并将返回的原始指针向下转换为派生的 class,然后插入到向量中。 (这段代码的核心是受到this thread的启发,但是这里省略了删除器的东西。)
请注意,Derived
是一个 class 模板这一事实在这里根本不重要(此外,您必须将 Derived</*some type*/>
而不是 Derived
传递给 getData
).
我有以下情况:
Base
是基数 class。
T
是一个模板,可以假定 Base
的任何派生 class。
底层为我提供来自 Base
class 的数据,我需要将其转换为上层(编写代码的那一层)上的特定 class在用户级别上工作。
代码如下:
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;
}
如何有效地将接收到的 Base
unique_ptr´s
class 的 vector
转换为 [=[=] unique_ptr´s
的 vector
16=] 如图所示键入。
感谢您的帮助。
这样做:
struct Base {};
template<typename T>
struct Derived : public Base {};
template <typename T>
std::vector<std::unique_ptr<T> > getData()
{
//add some checking:
static_assert(std::is_base_of<Base, T>::value, "T must be derived from Base");
std::vector<std::unique_ptr<Base> > retData;
//fill it somehow
std::vector<std::unique_ptr<T> > retCastData;
for (auto& item : retData)
{
auto t = std::unique_ptr<T>(static_cast<T*>(item.release())); //(*)
retCastData.push_back(std::move(t));
}
return retCastData;
}
int main()
{
getData<Derived<int> >(); //or some other type than "int"
}
主要的事情发生在标有(*)
的行中。这里释放了唯一指针,并将返回的原始指针向下转换为派生的 class,然后插入到向量中。 (这段代码的核心是受到this thread的启发,但是这里省略了删除器的东西。)
请注意,Derived
是一个 class 模板这一事实在这里根本不重要(此外,您必须将 Derived</*some type*/>
而不是 Derived
传递给 getData
).