使用 sizeof(boost::lockfree::queue<std::string>) 时出错

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

我收到一个关于 sizeof(boost::lockfree::queuestd::string) 的错误。我的代码如下:

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

编译时出现错误:

/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>)];

我不知道如何解决这个问题。有人可以提供一些帮助吗?谢谢。

lockfree::queue<T> 要求 T 有一个简单的析构函数(除其他要求外,请参阅 here

std::string 不是 trivial。因此 queue<std::string> 格式错误,您的 reinterpret_cast 技巧将无济于事。