简体   繁体   English

boost :: interprocess :: basic_string as std :: string

[英]boost::interprocess::basic_string as std::string

I am trying to replace a class method which returns const std::string & with const boost::interprocess::basic_string & . 我试图替换一个返回const std :: string&const boost :: interprocess :: basic_string&的类方法。 The main challenge I am facing is the incompatibility between the two classes despite their implementation similarity. 我面临的主要挑战是两个类之间的不兼容性,尽管它们的实现相似。 For more clear explanation I will put that into code 为了更清楚的解释,我将把它放入代码中

class A
{ std::string m_str;
 const std::string & StrVal() { return m_str; }
}

Now this class has to look like this: 现在这个类看起来像这样:

typedef boost::interprocess::allocator<char,boost::interprocess::managed_shared_memory::segment_manager> ShmemAllocatorChar;
typedef boost::interprocess::basic_string<char, std::char_traits<char>,ShmemAllocatorChar> ShMemString;

class A
{
 ShMemString m_str;
 const ShMemString & StrVal() { return m_str; }
}

The problem is that we have a huge code base depending on this: 问题是我们有一个庞大的代码库,具体取决于:

A a;
const std::string & str = a.StrVal();
// Many string specific operations go here, comparing str with other std::strings for instance

Even If I go over all the code replacing the expected results with const ShMemString &, it will be an even harder work to also fix the uses that follow. 即使我使用const ShMemString&替换所有代码替换预期结果,也将更难以修复后面的用途。 I was surprised to find out that the boost's string does not include any comparison/construction methods from std::string. 我很惊讶地发现boost的字符串不包含std :: string的任何比较/构造方法。

Any ideas on how to approach this? 关于如何处理这个的任何想法?

Even if boost::interprocess::basic_string<> did have a conversion to std::basic_string<> , it would be completely useless for your purposes -- after the conversion, the interprocess string would be destroyed, and its allocator is the important one (ie, the one holding the data in shared memory, which I assume is your motivation for switching basic_string<> implementations in the first place). 即使boost::interprocess::basic_string<>确实转换为std::basic_string<> ,它对你的目的来说也是完全无用的 - 转换后,进程间字符串将被破坏, 分配器是重要的一个(即,在共享内存中保存数据的那个,我假设这是你首先切换basic_string<>实现的动机)。

So, in the end, you have no choice but to go over all the code replacing the expected results with ShMemString const& (or auto const& if your compiler is recent enough to support it). 所以,最后,你别无选择,只能用ShMemString const& (或者auto const&如果你的编译器最近足以支持它)来替换所有代码。


To make this less painful in the future, typedef judiciously: 为了使这个痛苦少,今后, typedef明智:

struct A
{
    typedef ShMemString StrValType;
    StrValType const& StrVal() { return m_str; }
private:
    StrValType m_str;
};

// ...

A a;
A::StrValType const& str = a.StrVal();

This way, only the typedef inside of A needs to change and all code relying on it will automatically use the correct type. 这样,只需要更改A内部的typedef ,依赖它的所有代码都将自动使用正确的类型。

The problem is that we have a huge code base depending on this: 问题是我们有一个庞大的代码库,具体取决于:

Why does A::StrVal in the second one return an interprocess::basic_string ? 为什么第二个中的A::StrVal返回一个interprocess::basic_string It is an implementation detail of the class A that it uses interprocess::basic_string internally. 它是A类的一个实现细节,它在内部使用interprocess::basic_string The actual string class it's interface uses does not have to be the same. 它的接口使用的实际字符串类不必相同。 This is simply poor refactoring. 这简直就是糟糕的重构。

A::StrVal should return a std::string , just like always (well, not a const& of course, but user code won't need to change because of that). A::StrVal应该像往常一样返回一个std::string (当然,不是const& ,但用户代码不需要因此而改变)。 And therefore, A::StrVal will need to do the conversion between the two string types. 因此, A::StrVal需要在两种字符串类型之间进行转换。 That's how proper refactoring is done: you change the implementation, but the interface stays the same. 这就是如何进行适当的重构:您更改了实现,但界面保持不变。

Yes, this means you're going to have to copy the string data. 是的,这意味着您将不得不复制字符串数据。 Live with it. 和它一起生活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM