如何处理在使用自定义分配的字符串时涉及不同类型的情况?

huangapple go评论56阅读模式
英文:

How does one deal with different types when using custom allocated strings?

问题

我已经开始在一个视频游戏项目中尝试使用自定义分配器与 std::basic_string。这是我第一次涉及自定义分配器,所以我正在学到很多东西。

但问题是,我想能够调用任何接受 "string" 参数的函数,使用任何 std::basic_string(比如 std::stringstd::basic_string<char, std::char_traits<char>, custom_allocator<char> 等等)。

然而,提供一个自定义分配器会 "改变" std::basic_string 的类型(我的意思是,它与我一直在使用的 std::string 不同)。

所以我考虑了不同的解决方案:

  • 在函数中使用模板编程以接受 T,但是到处使用模板似乎有点繁琐和过度复杂。
  • 到处切换到 C 风格的字符串,并提供 my_string.c_str(),但考虑到失去了 C++ 的特性,这似乎是一个非常糟糕的主意。
  • 实现自己的 String 基类,并使所有的 CustomAllocatedString 都从它继承。这是一个个人项目,所以我不介意尝试,但我知道实现自己的 "STL" 可能非常耗时(还有其他问题)。
  • 在我的研究过程中,我了解到了多态分配器和 std::pmr::string(我使用的是 C++ 17)。尽管这似乎包含了内存开销(以及间接性),但它非常有趣。
  • 我是不是忽略了一些非常明显的东西?

你对此有什么看法?或者最终在一个类型中混合不同的分配方法是一个非常糟糕的主意吗?

英文:

I have started experimenting using custom allocators with std::basic_string in a video game project. This is my first time dealing with custom allocators, so I'm learning a lot.

But the problem is, I would like to be able to call any function taking a "string" parameter with any std::basic_string (std::string, std::basic_string<char, std::char_traits<char>, custom_allocator<char>, etc.).

However, providing a custom allocator "changes" the type of std::basic_string (I mean, it differs from std::string that I've been using).

So I thought of different solutions:

  • Use template programming in functions to take a T instead... but using templates everywhere seems a bit tedious and overkill...
  • Switch to C-style strings everywhere, and provide my_string.c_str(), which seems like a very bad idea, considering the loss of C++ features.
  • Implement my own String base class and make all the CustomAllocatedString inherit from it. This is for a personal project, so I don't mind experimenting, but I know that implementing my own "STL" can be very time consuming (among other things).
  • During my research, I learned about polymorphic allocators and std::pmr::string (I use C++ 17). Although this seems to include memory overhead (plus the indirection), it's really interesting.
  • Am I missing something really obvious here?

What do you think about this? Or is mixing different allocation methods with a single type a really bad idea in the end?

答案1

得分: 2

The solution might be to use string_view instead; that way functions don't have to deal with how strings are allocated.

#include <iostream>
#include <string>
#include <string_view>

void foo(const std::string_view str) 
{ 
    std::cout << str; 
}

int main() 
{
    std::string s = "Hello";
    std::pmr::string s2 = "World";
    foo(s);
    foo(" ");
    foo(s2);
    foo("\n");
}
英文:

The solution might be to use string_view instead; that way functions don't have to deal with how strings are allocated.

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;string_view&gt;

void foo(const std::string_view str) 
{ 
    std::cout &lt;&lt; str; 
}

int main() 
{
    std::string s = &quot;Hello&quot;;
    std::pmr::string s2 = &quot;World&quot;;
    foo(s);
    foo(&quot; &quot;);
    foo(s2);
    foo(&quot;\n&quot;);
}

huangapple
  • 本文由 发表于 2023年5月22日 04:20:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301779.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定