I've been shown that a std::string
cannot be inserted into a boost::lockfree::queue
.
boost::lockfree::queue
is too valuable to abandon, so I think I could use very large, fixed length char
s to pass the data according to the requirements (assuming that even satifies since I'm having trouble learning about how to satisfy these requirements), but that will eat up memory if I want large messages.
Does a dynamically-sized text object with a copy constructor, a trivial assignment operator, and a trivial destructor exist? If so, where? If not, please outline how to manifest one.
Dynamically sized, no. For something to have a trivial destructor, it requires that the destructor of the object is implicit (or defaulted), and any non-static member objects also have implicit (or defaulted) destructors. Since anything that is dynamically allocated will require a
delete []
somewhere along the line in a destructor, you cannot have this constraint satisfied.To expand upon the above, consider a (very cut down) example of what happens in
std::string
:Consider what would happen if we left the destructor as default: it would destroy the stack-allocated
char *
(that is, the pointer itself, not what it points to). This would cause a memory leak, as we now have allocated space that has no references and hence can never be freed. So we need to declare a destructor:However, by doing this, the destructor becomes user-defined and is therefore non-trivial.
This will be a problem with anything that is dynamically allocated. Note that we cannot fix this by using something like a
shared_ptr
or avector<char>
as a member variable; even though they may be stack allocated in our class, underneath, they are simply taking care of the memory management for us: somewhere along the line with these, there is anew []
and correspondingdelete []
, hence they will have non-trivial destructors.To satisfy this, you'll need to use a stack allocated
char
array. That means no dynamic allocation, and therefore a fixed size.A dynamically-size type with a trivial copy ctor/dtor is not possible. There are two solutions to your problem, use a fixed sized type, or store pointers in the queue: