Project

General

Profile

Bug #1443

Serialization for std::unique_ptr Fails With Abstract Base Class

Added by Nils Deppe about 1 month ago. Updated 19 days ago.

Status:
New
Priority:
Normal
Assignee:
Category:
-
Target version:
-
Start date:
02/22/2017
Due date:
% Done:

0%


Description

Attempting to serialize a std::unique_ptr fails when serializing an abstract base class with the error:


include/pup_stl.h:109:23: error: allocating an object of
abstract class type 'EmbeddingMap<3, 3>'
ptr.reset(new T);

This is because you are not able to call new on an abstract base class. One solution that we have used is:

@
template <typename T, typename std::enable_if<not std::is_base_of<
PUP::able, T>::value>::type* = nullptr>
inline void pup(PUP::er& p, std::unique_ptr<T>& t) {
bool is_nullptr = nullptr == t;
p | is_nullptr;
if (not is_nullptr) {
T* t1;
if (p.isUnpacking()) {
t1 = new T;
} else {
t1 = t.get();
}
p | *t1;
if (p.isUnpacking()) {
t.reset(t1);
}
}
}

template <typename T,
typename std::enable_if<std::is_base_of<PUP::able, T>::value>::type* =
nullptr>
inline void pup(PUP::er& p, std::unique_ptr<T>& t) {
T* t1;
if (p.isUnpacking()) {
p | t1;
t = std::unique_ptr<T>(t1);
} else {
t1 = t.get();
p | t1;
}
}

template <typename T>
inline void operator|(PUP::er& p, std::unique_ptr<T>& t) {
pup(p, t);
}
@

I think the lower level issue might be that it only really makes sense to serialize an abstract base class if the receiving pointer is of the same concrete derived class.

History

#1 Updated by Eric Bohm 19 days ago

  • Assignee set to Eric Mikida

Also available in: Atom PDF