Search code examples
c++boost

an error while use sizeof(boost::lockfree::queue<std::string>)


I got an error about sizeof(boost::lockfree::queuestd::string). My code is as below:

Global.h:

extern boost::lockfree::queue<std::string> &imgPendingQueue;

class Initializer
{
// do something
};

Global.cpp:

#include "Global.h"

static char imgPendingQueueBuf[sizeof(boost::lockfree::queue<std::string>)];

boost::lockfree::queue<std::string> &imgPendingQueue = reinterpret_cast<boost::lockfree::queue<std::string>&>(imgPendingQueueBuf);

// do something

While compiling, I got an error:

/usr/local/include/boost/lockfree/queue.hpp:95:5: error: static_assert failed due to requirement 'boost::has_trivial_destructor<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>::value' "(boost::has_trivial_destructor<T>::value)"
    BOOST_STATIC_ASSERT((boost::has_trivial_destructor<T>::value));
    ^                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/boost/static_assert.hpp:70:41: note: expanded from macro 'BOOST_STATIC_ASSERT'
#     define BOOST_STATIC_ASSERT( ... ) static_assert(__VA_ARGS__, #__VA_ARGS__)
                                        ^             ~~~~~~~~~~~
Global.cpp:8:32: note: in instantiation of template class 'boost::lockfree::queue<std::__1::basic_string<char>>' requested here
static char imgPendingQueueBuf[sizeof(boost::lockfree::queue<std::string>)];

I don't know how to solve the problem. Could somebody provide some help? Thank you.


Solution

  • lockfree::queue<T> requires T to have a trivial destructor (among other requirements, see here)

    std::string is not trivial. Hence queue<std::string> is ill-formed and your reinterpret_cast trick will not help.