gpt4 book ai didi

c++ - boost circular_buffer push_back 编译错误

转载 作者:太空狗 更新时间:2023-10-29 21:42:53 45 4
gpt4 key购买 nike

我正在实现 boost::circular_bufferboost::managed_shared_memory .我使用示例代码演示共享内存中的 vector here .我做了以下更改:

1) typedef boost::circular_buffer<int, ShmemAllocatorCB> MyCircularBuffer;

2) MyCircularBuffer *circbuff = segment.construct<MyCircularBuffer>("MyCB")(alloc_inst);

3) circbuff->push_back(1);

代码在第 3 行给出编译错误。错误是

error C2665: 'operator new' : none of the 5 overloads could convert all the argument types c:\boost2005\boost\circular_buffer\base.hpp 1470

来自documentation ,我知道 push_back 函数需要以下 3 种形式之一:

void push_back(param_value_type);
void push_back(rvalue_type);
void push_back();

我尝试了空参数调用,尝试将 1 转换为 param_value_type、rvalue_type,但它们似乎都不起作用。这可能是一个简单的错误,但我已经有一段时间没弄清楚了。任何帮助表示赞赏。谢谢。

编辑:

typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocatorCB;

managed_shared_memory segment(create_only, "MySharedMemory", 65536);

const ShmemAllocatorCB alloc_inst (segment.get_segment_manager());

最佳答案

看起来 Boost 的循环缓冲区在某个时间点使用原始指针进行调试。不幸的是,这是禁止的:

Boost 提供了一些关于这个主题的引用:

总而言之,通过定义 BOOST_CB_DISABLE_DEBUGNDEBUG 预处理器定义来编译没有 circular_buffer 调试支持的代码。

编辑:这是一个工作示例:

(使用 BOOST_LIB_VERSION = 1_49 测试)

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/circular_buffer.hpp>
#include <iostream>
#include <string>
#include <cstdlib> //std::system

using namespace boost::interprocess;

//Define an STL compatible allocator of ints that allocates from the managed_shared_memory.
//This allocator will allow placing containers in the segment
typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocatorCB;

//Alias a vector that uses the previous STL-like allocator so that allocates
//its values from the segment
typedef boost::circular_buffer<int, ShmemAllocatorCB> MyCircularBuffer;

//Main function. For parent process argc == 1, for child process argc == 2
int main(int argc, char *argv[])
{
if(argc == 1){ //Parent process
//Remove shared memory on construction and destruction
struct shm_remove
{
shm_remove() { shared_memory_object::remove("MySharedMemory"); }
~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
} remover;

//Create a new segment with given name and size
managed_shared_memory segment(create_only, "MySharedMemory", 65536);

//Initialize shared memory STL-compatible allocator
const ShmemAllocatorCB alloc_inst (segment.get_segment_manager());

//Construct a buffer named "MyCB" in shared memory with argument 10 (for size) and alloc_inst
MyCircularBuffer *circbuff = segment.construct<MyCircularBuffer>("MyCB")(10, alloc_inst);

for(int i = 0; i < 100; ++i) //Insert data in the buffer, with overflows
circbuff->push_back(i);

//Launch child process
std::string s(argv[0]); s += " child ";
if(0 != std::system(s.c_str()))
return 1;

//Check child has destroyed the buffer
if(segment.find<MyCircularBuffer>("MyCB").first)
return 1;
}
else{ //Child process
//Open the managed segment
managed_shared_memory segment(open_only, "MySharedMemory");

//Find the buffer using the c-string name
MyCircularBuffer *circbuff = segment.find<MyCircularBuffer>("MyCB").first;

//Use buffer in reverse order
std::cout << "Child got: " << (*circbuff)[3] << "\n"; // 93

//When done, destroy the buffer from the segment
segment.destroy<MyCircularBuffer>("MyCB");
}

return 0;
};

要遵守它:

g++ -DBOOST_CB_DISABLE_DEBUG -DNDEBUG cb_ipc.cpp -o cb_ipc -lboost_system -lrt

运行它:

./cb_ipc
Child got: 93

关于c++ - boost circular_buffer push_back 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24485218/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com