与 boost::interprocess 共享内存时从 std::string 转换为 "MyShmString"

Converting from std::string to "MyShmString" when sharing memory with boost::interprocess

我关注此 boost::interprocess guide and this Stack Overflow thread 是为了在不同进程之间共享 std::string 变量。具体来说,我有一个函数接收一个 std::string 应该在其他进程之间共享:

void registerFailedTests(std::string failed_test_name) {

    boost::interprocess::permissions perm;
    perm.set_unrestricted();

    boost::interprocess::shared_memory_object::remove(unit_tests_constants::shm_failed_tests_list.c_str());
    boost::interprocess::managed_shared_memory managed_shm_failed_tests_list(boost::interprocess::open_or_create, unit_tests_constants::shm_failed_tests_list.c_str(), 10240, 0, perm);

    typedef boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> CharAllocator;
    typedef boost::interprocess::basic_string<char, std::char_traits<char>, CharAllocator> myShmString;

    CharAllocator charallocator(managed_shm_failed_tests_list.get_segment_manager());

    myShmString mystring(charallocator);
}

问题是参数 failed_test_name 是一个 std::string,我无法将其转换为 myShmString 类型以便在共享内存中分配它。

我怎样才能做到这一点?

非常感谢。

只需使用从输入序列复制的任何构造函数:

myShmString mystring(failed_test_name.begin(), failed_test_name.end(), charallocator);